java - 按下 '.' 时按 Tab 键切换到 Eclipse JFace 对话框中的下一个字段

标签 java swt jface

我有一个 Eclipse JFace Dialog,其中包含多个 Text 字段。当用户在文本字段中按下 . 键时,我希望它的行为就像按下了 Tab 键一样,并将焦点发送到下一个文本字段,然后将 . 丢弃(想象一下输入 IP 地址)。

如何最好地实现这一点?

最佳答案

下面的内容就是你想要的。基本上,它将 KeyListenerFocusListener 添加到 Text 小部件,并在按下 . 时循环遍历它们:

public class TestClass
{
    private static int counter = 0;

    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);

        shell.setLayout(new FillLayout());

        /* These are the text fields */
        final Text textOne = new Text(shell, SWT.BORDER);
        final Text textTwo = new Text(shell, SWT.BORDER);
        final Text textThree = new Text(shell, SWT.BORDER);

        /* Save them in an arraylist */
        final ArrayList<Text> textFields = new ArrayList<Text>();
        textFields.add(textOne);
        textFields.add(textTwo);
        textFields.add(textThree);

        /* save their position as well (not optimal, you can think of an easier method) */
        final HashMap<Text, Integer> textFieldsMapping = new HashMap<Text, Integer>();
        textFieldsMapping.put(textOne, 0);
        textFieldsMapping.put(textTwo, 1);
        textFieldsMapping.put(textThree, 2);

        /* Define keylistener which takes care of using . as tab */
        KeyListener keyListener = new KeyListener() {

            @Override
            public void keyReleased(KeyEvent arg0) {
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
                /* if '.' pressed */
                if(arg0.character == '.')
                {
                    /* ignore this action */
                    arg0.doit = false;

                    /* get next text field */
                    Text next = textFields.get(counter);

                    /* force focus on this text field */
                    next.setFocus();
                    next.forceFocus();
                }
            }
        };

        /* Define focuslistener which sets current position */
        FocusListener focusListener = new FocusListener() {

            @Override
            public void focusLost(FocusEvent arg0) {
            }

            @Override
            public void focusGained(FocusEvent arg0) {
                /* get current text field */
                Text current = (Text)arg0.getSource();

                /* get current position */
                int currentPosition = textFieldsMapping.get(current);

                /* set counter to next text field */
                counter = (currentPosition + 1) % textFields.size();
            }
        };

        /* add keylistener to text fields */
        textOne.addKeyListener(keyListener);
        textTwo.addKeyListener(keyListener);
        textThree.addKeyListener(keyListener);

        /* add focuslistener to text fields */
        textOne.addFocusListener(focusListener);
        textTwo.addFocusListener(focusListener);
        textThree.addFocusListener(focusListener);

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

关于java - 按下 '.' 时按 Tab 键切换到 Eclipse JFace 对话框中的下一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652312/

相关文章:

java - isDisplayed 对于特定元素始终返回 false

java - FacesServlet URL 模式

java - Java中的循环数据结构

java - 使用 Maven 在 OSX 上出现 SWT 错误

java - Linux从java中查找命令不起作用

java - SWT 文本中上下文菜单的子菜单

java - 如何停止在 Eclipse RCP 中打开 View 部分?

Java 7 使用 Web Start 破坏了 OS X 上的 SWT 应用程序

java - 如何制作可编辑的 SWT 组合,允许用户仅键入组合中的项目