I have a function in a java class which gets triggers an action listener (as seen below):
// action event fired when hitting a checkbox
public void fireActionCheckBox(MyMainClass frame, JCheckBox theButtonExample) {
for(ActionListener a: theButtonExample.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
});
}
}
Then i have a second function which does the same for a JButton's action listener:
// action event fired when hitting a button
public void fireActionButton(MyMainClass frame, JButton theButtonExample) {
for(ActionListener a: theButtonExample.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
});
}
}
I understand that in java the arguments must be assigned before it begins, but it seems inefficient to write the same code twice. Is there any better way to do this they would allow me not to write two functions for an action which is so similar.
Thank you for your help!

JButtonandJCheckBox?JComponentis the parent class ofJButtonandJCheckBoxbut i can not use.getActionListeners()with aJComponentAbstractButton