Java SWT文本侦听器

Java SWT文本侦听器,java,text,swt,Java,Text,Swt,我在SWT中有一个文本: final Text textArea = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL); textArea.setVisible(false); textArea.setEditable(false); textArea.setEnabled(false); textArea.setText("Scheduler Info"); 我有一个听众。一旦启动了侦听器,我希望文本区域中的一些数据能够一次又一次地覆

我在SWT中有一个
文本

final Text textArea = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
textArea.setVisible(false);
textArea.setEditable(false);
textArea.setEnabled(false);
textArea.setText("Scheduler Info");

我有一个听众。一旦启动了侦听器,我希望文本区域中的一些数据能够一次又一次地覆盖。我是否可以在文本区域保留“Scheduler Info”标题。我不希望第一行被覆盖。我希望覆盖该区域的其余部分。

有两种方法可以做到这一点:

  • 只需将
    Text#setText(String)
    与新的
    String
    一起使用,并在原始字符串前面加上前缀
  • 选择原始字符串后的所有内容,然后插入新内容
  • 以下是两种方法的示例:

    private static final String INITIAL_TEXT = "Scheduler Info";
    
    public static void main(String[] args)
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("StackOverflow");
        shell.setLayout(new FillLayout());
    
        final Text text = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
        text.setEditable(false);
        text.setEnabled(false);
        text.setText(INITIAL_TEXT);
    
        Button replace = new Button(shell, SWT.PUSH);
        replace.setText("Replace");
        replace.addListener(SWT.Selection, new Listener()
        {
            private int counter = 1;
            @Override
            public void handleEvent(Event arg0)
            {
                String replace = INITIAL_TEXT;
    
                for(int i = 0; i < counter; i++)
                    replace += "\nLine " + i;
    
                text.setText(replace);
    
                counter++;
            }
        });
    
        Button insert = new Button(shell, SWT.PUSH);
        insert.setText("Insert");
        insert.addListener(SWT.Selection, new Listener()
        {
            private int counter = 1;
            @Override
            public void handleEvent(Event arg0)
            {
                text.setSelection(INITIAL_TEXT.length(), text.getText().length());
    
                String newText = "";
    
                for(int i = 0; i < counter; i++)
                    newText += "\nLine " + i;
    
                text.insert(newText);
    
                counter++;
            }
        });
    
        shell.pack();
        shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
        shell.open();
    
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    } 
    
    private static final String INITIAL\u TEXT=“Scheduler Info”;
    公共静态void main(字符串[]args)
    {
    最终显示=新显示();
    最终外壳=新外壳(显示);
    shell.setText(“StackOverflow”);
    setLayout(新的FillLayout());
    最终文本文本=新文本(shell,SWT.MULTI | SWT.WRAP | SWT.V|u滚动);
    text.setEditable(false);
    text.setEnabled(false);
    text.setText(初始文本);
    按钮更换=新按钮(外壳,SWT.PUSH);
    替换。setText(“替换”);
    replace.addListener(SWT.Selection,new Listener())
    {
    专用整数计数器=1;
    @凌驾
    公共无效handleEvent(事件arg0)
    {
    字符串替换=初始文本;
    对于(int i=0;i
    感谢您回答我有关SWT的所有问题。