How do I programmatically send ActionEvent to JButton?

前端 未结 5 1967
太阳男子
太阳男子 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:36

    Only if you inherit and expose the fireActionPerformed method, which is protected:

    class MockButton extends JButton { 
       // bunch of constructors here 
       @Override 
       public void fireActionPerformed( ActionEvent e ) { 
           super.fireActionPerformed( e );
       }
    }
    

    Then you will be able, but of course, you have to use a reference like this:

    MockButton b = .... 
    
    b.fireActionPerformed( new Action... etc. etc
    

    Why would you like to do that? I don't know, but I would suggest you to follow Mark's advice

提交回复
热议问题