?
Solved

Performing the same action on many JButtons at once

Posted on 2004-10-01
8
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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