Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 01:41 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    Atlan (01-02-2013)

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ah ha! That did it! Thanks a bunch.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •