How do I programmatically send ActionEvent to JButton?

前端 未结 5 1834
太阳男子
太阳男子 2020-12-20 11:56

How do I programmatically send an ActionEvent (eg button pressed/ACTION_PERFORMED) to a JButton?

I know of:

button.doClick(         


        
5条回答
  •  时光说笑
    2020-12-20 12:18

    You can get a button's ActionListeners, and then call the actionPerformed method directly.

    ActionEvent event;
    long when;
    
    when  = System.currentTimeMillis();
    event = new ActionEvent(button, ActionEvent.ACTION_PERFORMED, "Anything", when, 0);
    
    for (ActionListener listener : button.getActionListeners()) {
        listener.actionPerformed(event);
    }
    

提交回复
热议问题