How do you simulate a click on a JTextField? Equivalent of JButton doClick()?

前端 未结 4 2070
清歌不尽
清歌不尽 2021-01-16 20:19

I am working on a Java project and need to have a keypress simulate a click on a JTextField. What I\'m looking for is the equivalent of the JButton doClick() method.

4条回答
  •  醉酒成梦
    2021-01-16 21:04

    public void simulateKey(KeyEvent e, Component c) {
       Field f = KeyEvent.class.getField("focusManagerIsDispatching");
       f.setAccessible(true);
       f.set(e, Boolean.TRUE);
       c.dispatchEvent(e);
    }
    

    Send "Enter" to your JTextField. This was stolen from here.

提交回复
热议问题