Solved

Performing the same action on many JButtons at once

Posted on 2004-10-01
8
209 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
 
LVL 86

Expert Comment

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

isn't it

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

?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now