Solved

Performing the same action on many JButtons at once

Posted on 2004-10-01
8
219 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

695 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