等待JTextField“SPACE”事件

时间:2013-01-01 18:54:24

标签: java swing jtextfield keyevent

我有一个带有JTextField的JPanel,我想实现waitForTextFieldSpace。我认为制作一个自定义的JTextField将是最优雅的解决方案,但我并不完全确定。

public class MyPanel {
   JTextField textField;

   //...constructor, methods, etc

   public void waitForTextFieldSpace() {
      //...don't return until the user has pressed space in the text field
   }
}

我确实有一些代码有一个JFrame等待空格键。但我不确定如何进行上面的文本字段任务。

public void waitForKey(final int key) {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == key) {
                    latch.countDown();
                }
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        try {
            //current thread waits here until countDown() is called (see a few lines above)
            latch.await();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }  
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);

    }

3 个答案:

答案 0 :(得分:4)

  • Keylistener永远不要使用JTextComponets(没有真正的理由,例如确定3个或更多键被按下),使用DocumentDocumentListener,{ {1}}

  • 使用KeyBinding作为标准密钥,通知 - 必须检查API中是否需要密钥(key_shortcut)

  • 在这种情况下使用DocumentFilterPatern

简单的例子

DocumentFilter

答案 1 :(得分:0)

您是否考虑过使用关键事件监听器?您可以将其添加到文本字段或区域,并检查它是否有特定的键。我将它用于输入或制表符之类的内容,这样当用户按下一个时,它就会启动差异操作。

Java Key Listener

Document Listener会为您工作吗?每次文本字段中的值更改时都会检查。

Document Listener

以下是我在文本字段中执行操作时的示例代码,以便在按下某个键时执行操作。

textField.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(final java.awt.event.KeyEvent evt) {
        textFieldKeyPressed(evt);
    }
});

private void textFieldKeyPressed(final java.awt.event.KeyEvent evt) {
    final int key = evt.getKeyCode();
    if (key == KeyEvent.VK_ENTER || key == KeyEvent.VK_TAB) {
        if (!textField.getText().equals("Updating")) {
            loadTable();
        }
    }
}

答案 2 :(得分:0)

正确的解决方案是按如下方式创建自定义JTextField

public class MyTextField extends JTextField {

    private static final long serialVersionUID = 1833734152463502703L;

    public MyTextField() {

    }

    public void waitForKey(final int key) {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == key) {
                    latch.countDown();
                }
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        try {
            //current thread waits here until countDown() is called (see a few lines above)
            latch.await();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }  
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);

    }

}

然后在原始方法中,只需在MyTextField对象上调用waitForKey()