Solved

Performing the same action on many JButtons at once

Posted on 2004-10-01
8
212 Views
Last Modified: 2010-08-05
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
Comment
Question by:zaphod_beeblebrox
  • 5
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12198373
Use an array of buttons
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12198380
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12198386
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 86

Expert Comment

by:CEHJ
ID: 12198420
c.removeActionListener(al);

isn't it

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

?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12198423
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
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12198436
>> ((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
 

Author Comment

by:zaphod_beeblebrox
ID: 12198477
Thanks zzynx (and CEHJ).

Perfect answers as usual
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12198494
:)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question