Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually invoke an Action in swing?

Tags:

java

action

swing

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 work with. Now I'm stuck with one little thing: How do I run them manually? I mean by code? Note that I am building the GUI using Netbeans (if that makes any difference). I've come as far as:

Application a = Application.getInstance(JPADemoApp.class); ApplicationContext ctx = a.getContext(); ActionMap am = ctx.getActionMap(JPADemoView.class, this.app); Action act = am.get("fetchOrders"); 

( I wrote all on separate lines to simplify debugging )

So now I have a valid reference to the Action. Now how do I run it?

like image 335
exhuma Avatar asked Jun 20 '10 14:06

exhuma


People also ask

What are action commands in Java?

Java lets us specify an “action command” string for buttons (and other components, like menu items, that can generate action events). The action command is less interesting than it sounds. It is just a String that serves to identify the component that sent the event.


1 Answers

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     }); } 
like image 56
arkon Avatar answered Sep 16 '22 19:09

arkon