0

I have a actionSave class which extends AbstractAction.I use it for save button. somewhere else i want to run the same instant of it which was used for the button. I came to conclusion to use it as below but i do not know what to pass as argument?

model.getActionSave().actionPerformed("what should i add here for action event");
itro
  • 7,006
  • 27
  • 78
  • 121

2 Answers2

4

Extract the code of the actionPerformed() method into another method without argument, and call this method instead:

public void actionPerformed(ActionEvent e) {
    save();
}

public void save() {
    ...
}

...

model.getActionSave().save();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Just create your own ActionEvent, it has a public constructor. E.g.

model.getActionSave().actionPerformed( 
 new ActionEvent( this, ActionEvent.ACTION_PERFORMED, "Save" )
);

If you are e.g. testing your UI, you can also opt to perform a click on the button through the API:

button.doClick();

But in general I prefer the first approach, and avoid the coupling with the UI

Robin
  • 36,233
  • 5
  • 47
  • 99