Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2012, 10:53 PM   PM User | #1
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
Linking an ActionListener to an array

Hi again

I've got a lot of mostly identical JButtons (60), which I put into an array, to save time.

Code:
JButton[] arsenalSel = new JButton[60];
which I add with a For loop

Code:
GridBagConstraints c = new GridBagConstraints();
		 c.anchor = GridBagConstraints.WEST; //Align to the left?
		 for(int i =0; i < 60; i++)
		 {
			 String tempwep = "";

			 tempwep = "" + wMaster.allWeapons[i].name;
			 arsenalSel[i] = new JButton(tempwep+"");
			 c.gridx = 1; c.gridy = i; parsn.add(arsenalSel[i], c);
              }
This gives me an array of 60 buttons which get their contents from another object called wMaster, which are added to a JPanel.

My Problem: I need an actionlistener for each button. I don't want to have to do 60 of them.
What I need is some sort of actionlistener which listens to the whole array at once. Failing that, I need a way to do use a loop to make actionlisteners.
Any ideas?

Last edited by Atlan; 12-30-2012 at 12:41 AM..
Atlan is offline   Reply With Quote
Old 12-30-2012, 01:35 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
As you've said, a loop to apply an action listener to each of the buttons.
An alternative could be to use a JList and render it to look and act like button's instead. This would only require one listener since each of the items is a part of the JList collection and not a separate component like an array of JButton is.
Fou-Lu is offline   Reply With Quote
Old 12-30-2012, 07:31 PM   PM User | #3
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
I tried using a loop, but I ran into a problem.

Code:
for(int wb = 0; wb < 60; wb++)
			 {
				 arsenalSel[wb].addActionListener(new ActionListener()
				 {
					 public void actionPerformed(ActionEvent e)
					 {
						System.out.println("" +wMaster.allWeapons[wb].name);
					 }
				 });
			 }
It won't let me refer to wb, because it's not a 'final'. If I change wb TO final, the rest of the code breaks. The next actionlistener stops working, with eclipse saying it's 'Unreachable Code'.

Any idea how to fix that?
Atlan is offline   Reply With Quote
Old 12-31-2012, 01:47 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can't reach inside of the array itself this way. Applying the listener isn't the problem, the referencing it as an array is since the listeners have no idea you are referring to many possible items.
If you want to do it this way, I'd say the easiest approach is to use the setActionCommand on the JButton (also in the loop), and add the array offset to the action command. That can be retrieved within the actionlistener.
PHP Code:
    public static void main(String[] argv)
    {
        
SwingUtilities.invokeLater(new Runnable(){
            @
Override
            
public void run()
            {
                
JFrame jf = new JFrame();
                final 
String[] allWeapons = new String[5];
                
allWeapons[0] = "a";
                
allWeapons[1] = "b";
                
allWeapons[2] = "c";
                
allWeapons[3] = "d";
                
allWeapons[4] = "e";
                
                
ActionListener al = new ActionListener()
                {
                    @
Override
                    
public void actionPerformed(ActionEvent e)
                    {
                        try
                        {
                            
Integer ic Integer.parseInt(e.getActionCommand());
                            
JOptionPane.showMessageDialog(null"Clicked on " allWeapons[ic]);    
                        }
                        catch (
NumberFormatException ex)
                        {
                        }
                    }
                };
                
                for (
int i 0allWeapons.length; ++i)
                {
                    
JButton jb = new JButton(allWeapons[i]);
                    
jb.setActionCommand(Integer.toString(i));
                    
jb.addActionListener(al);
                    
jf.getContentPane().add(jb);
                    
                }
                
jf.setLayout(new GridLayout(15));
                
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
jf.pack();
                
jf.setVisible(true);
            }
        });
    } 
Like that.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Atlan (01-02-2013)
Old 01-02-2013, 07:53 PM   PM User | #5
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
Ah ha! That did it! Thanks a bunch.
Atlan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:10 AM.


Advertisement
Log in to turn off these ads.