How do I manually invoke an Action in swing?

前端 未结 3 1714
遇见更好的自我
遇见更好的自我 2020-12-10 10:11

For the life of me I cannot seem to find details on Java Swing Actions :\'( When I came across them I immediately realised their usefulness. So far it\'s all been easy to wo

相关标签:
3条回答
  • 2020-12-10 10:51

    If you want to run your action manually, you can generate an ActionEvent and pass it into the actionPerformed method that your Action must implement, as the Action interface extends ActionListener.

    0 讨论(0)
  • 2020-12-10 10:51

    Because an Action is an EventListener, you may want to consider implementing an EventListenerList as a way to expose methods that fire actions.

    0 讨论(0)
  • 2020-12-10 11:14

    You can simply invoke the action event's method directly:

    for(ActionListener a: buttonExample.getActionListeners()) {
        a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null) {
              //Nothing need go here, the actionPerformed method (with the
              //above arguments) will trigger the respective listener
        });
    }
    
    0 讨论(0)
提交回复
热议问题