[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Performing the same action on many JButtons at once

Within an Applet, Image I have a JPanel, called NavPanel which contains a number of buttons, which are used to navigate around the Applet.

When the user wants to switch screens the NavPanel is cleared and a new set of JButtons are then put in situ.

The problem I have is that I have to remove the Action Listener for each button.

I am doing this per object e.g.
Button1.removeActionListener(this);
Button2.removeActionListener(this);
Button3.removeActionListener(this);
etc...

There must be a better way!

How can I set the method up so that it does something like this:

For Each Object in NavPanel
    Object.removeActionListener(this);
next

Bearing in mind the number of objects will vary
Anyone care to have a stab at this one?

Thanks guys

0
zaphod_beeblebrox
Asked:
zaphod_beeblebrox
  • 5
  • 2
1 Solution
 
CEHJCommented:
Use an array of buttons
0
 
zzynxSoftware engineerCommented:
Call this function with NavPanel as 1st parameter and this (being the actionlistener) as second parameter:

private void removeActionListenerRecursively(Component c, ActionListener al) {
        c.removeActionListener(al);
        if (c instanceof Container) {
            Container cont = (Container)c;
            cont.removeActionListener(al);
            Component[] children = cont.getComponents();
            for (int i = 0; i < children.length; i++) {
                removeActionListenerRecursively(children[i]);
            }
        }
}
0
 
zzynxSoftware engineerCommented:
If you want to be sure you remove the AL only for JButtons:

private void removeActionListenerRecursively(Component c, ActionListener al) {
        if (c instanceof JButton)  // <<<<< add this one
           c.removeActionListener(al);
        if (c instanceof Container) {
            Container cont = (Container)c;
                                                                    // <<<<<<<< removed a line
            Component[] children = cont.getComponents();
            for (int i = 0; i < children.length; i++) {
                removeActionListenerRecursively(children[i]);
            }
        }
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
c.removeActionListener(al);

isn't it

((JButton)c).removeActionListener(al);

?
0
 
zzynxSoftware engineerCommented:
Typo:
        removeActionListenerRecursively(children[i]);
should be:
        removeActionListenerRecursively(children[i], al);


For adding you can use this recursive function:

    private void addActionListenerRecursively(Component c, ActionListener al) {
        if (c instanceif JButton) {
              //To be on the safe side, try to remove the ActionListener first
              //just in case it has been added before. If not, it won't do any harm
             c.removeActionListener(al);
             //Add KeyListener to the Component passed as an argument
             c.addActionListener(al);
        }

        if (c instanceof Container) {
            Container cont = (Container)c;
            Component[] children = cont.getComponents();

            //For every child repeat the above operation.
            for (int i = 0; i < children.length; i++) {
                addActionListenerRecursively(children[i], al);
            }
        }
    }
0
 
zzynxSoftware engineerCommented:
>> ((JButton)c).removeActionListener(al);
Right you are.

So, I repeat ;°)

private void removeActionListenerRecursively(Component c, ActionListener al) {
        if (c instanceof JButton)
           ((JButton)c).removeActionListener(al);
        if (c instanceof Container) {
            Container cont = (Container)c;
            Component[] children = cont.getComponents();
            for (int i = 0; i < children.length; i++) {
                removeActionListenerRecursively(children[i], al);
            }
        }
}
0
 
zaphod_beeblebroxAuthor Commented:
Thanks zzynx (and CEHJ).

Perfect answers as usual
0
 
zzynxSoftware engineerCommented:
:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now