-1

当 a 的内容Text超过 的宽度时Text,我想...在文本字段的末尾显示点 ( )。

我尝试使用 aModifyListener没有成功。对此的任何帮助将不胜感激。

  @Override
        public void modifyText(ModifyEvent e) {
            // TODO Auto-generated method stub



           if (wakeupPatternText.getText().length()>=12) {


               String wakeuppattern=wakeupPatternText.getText(0, 11);




               String dot="...";

              String wakeup=wakeuppattern+dot;
 wakeupPatternText.setText(wakeup);




        }      


        }
    });
4

1 回答 1

1

这应该做你想要的:

private static String textContent = "";

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    Text text = new Text(shell, SWT.BORDER);

    text.addListener(SWT.FocusOut, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Text text = (Text) e.widget;
            textContent = text.getText();

            text.setText(textContent.substring(0, Math.min(10, textContent.length())) + "...");
        }
    });

    text.addListener(SWT.FocusIn, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Text text = (Text) e.widget;

            text.setText(textContent);
        }
    });

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Lose focus");

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

String它侦听焦点事件并在它太长时截断可见事件。

这是它的外观:

重点:

在此处输入图像描述

没有焦点:

在此处输入图像描述


您的代码无法正常工作的原因如下:当您wakeupPatternText.setText(wakeup);从侦听器内部调用时VerifyListener,侦听器本身会被再次递归调用。

于 2013-04-22T09:03:50.803 回答