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

前端 未结 4 2072
清歌不尽
清歌不尽 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:08

    What I want is when the user presses enter, it calls the same code as when they click anywhere in the frame

    Still doesn't make sense to me.

    When the user clicks any where on the frame a couple of things happen:

    a) the text field loses focus

    b) some other component gains focus

    You could add an ActionListener to the text field. The ActionListener is invoked when the Enter key is pressed. But then how to you guess where on the frame to generate a mouse click? Seems like random logic to me.

提交回复
热议问题