Programmatically clicking a GUI button in Java Swing

前端 未结 6 930
灰色年华
灰色年华 2020-12-13 23:06

How would I programmatically click a Swing JButton in a way that would register all the relevant action/mouse events and be visible to the user (i.e. they\'d see the button

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 00:05

    You could always simulate it by firing an action event with it as the source.

    http://download.oracle.com/javase/6/docs/api/java/awt/event/ActionEvent.html

    To fire it, create the action event above, and whatever listener you want just call

    ActionEvent e = new ActionEvent(myButton,1234,"CommandToPeform");
    myListener.actionPerformed(e);
    

提交回复
热议问题