Search code examples
javaactionlistenerlistenersactionevent

How can I fire an ActionEvent from a different JComponent?


I have a JTextField with an ActionListener registered to it. When this JTextField has focus, and the enter key is pressed, an ActionEvent is created.

How can I link this event to another components ActionListener, like a JButton, so I can run its code?


Solution

  • Have each action listener just call the same function. For example,

     public void actionPerformed( ActionEvent e ) {
        myModel.actOnButtonOrTextEvent( e );
     }