3

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!

6
  • 3
    Hint: Is there a common parent class between JButton and JCheckBox? Commented Apr 27, 2017 at 6:36
  • maybe you can palce the for loop in a individual class as a method,and then invoke then in this two place Commented Apr 27, 2017 at 6:40
  • @Joe C - yes JComponentis the parent class of JButton and JCheckBox but i can not use .getActionListeners() with a JComponent Commented Apr 27, 2017 at 6:43
  • 1
    try to use AbstractButton Commented Apr 27, 2017 at 6:44
  • @wylasr - thank you! i considered that and most likely will do that if i can not find a better way Commented Apr 27, 2017 at 6:45

3 Answers 3

3
public void fireActionCheckBox(MyMainClass frame, AbstractButton button) { ... }

There is an abstract class AbstractButton which is a parent of both of these classes. It has defined the getActionListeners method.

Moreover, you could rewrite the method in a more generic way:

public <T extends AbstractButton> void fireActionButton(MyMainClass frame, T button) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

What advantage is gained by writing it as the second example you have shown?
@JFreeman, it just looks more expressive, no difference until you use a collection of types with some hierarchy.
2

You could pass to method a generics parameter rather than JCheckBox theButtonExample and JButton theButtonExample. For instance, assume both of the classes extends the same parent, you could do

public <J extends commonParent> void fireActionButton(MyMainClass frame, J j) {
  //...
}

As @Sweeper indicated in the comments, since the parent does not have the listener, you will need to check type and do a downcasting:

public <J extends JComponent> void fireActionButton(MyMainClass frame, J j) {
  if (j instanceof JComboBox) {
    JCheckbox jbox = (JComboBox)j;
    // Do something else
  }
}

2 Comments

The problem is, the common parent of JButton and JComboBox - JComponent does not have a getActionListeners method.
You are right. I think in that case we need to check object type within the method. Still, I think it's better than writing the same method twice :P
1

both JCheckBox and JButton are childs of the same parent class:

enter image description here

define a method with the superclass of both:

public void fireActionAbstractButton(MyMainClass frame, AbstractButton myAbstractButton) {
        System.out.println(myAbstractButton.getClass().getName());
    }

1 Comment

Yes this works! (i unfortunately can only mark one answer as correct)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.