Eclipse plugin: make Combo to handle Enter key

我们两清 提交于 2019-12-08 06:24:33

问题


Eclipse plugin: how to make Combo to handle Enter key pressing? That is after entering some text user can press Enter, that will case some processing. The same processing can be started by Button "Run" click.

org.eclipse.swt.widgets.Combo


回答1:


Just add a Listener for SWT.KeyUp and check if the entered character is equal to SWT.CR.

Here is some code:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, true));

    final Combo combo = new Combo(shell, SWT.NONE);

    combo.addListener(SWT.KeyUp, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            if(arg0.character == SWT.CR)
                MessageDialog.openInformation(shell, "Input", "You entered: " + combo.getText());
        }
    });

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

Looks like this:

And will show this dialog when the user presses enter:



来源:https://stackoverflow.com/questions/17511442/eclipse-plugin-make-combo-to-handle-enter-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!