Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ActionListeners for an array of JComboBoxes...  (Read 2018 times)
0 Members and 1 Guest are viewing this topic.
Offline THEOWL

Senior Newbie




Java games rock!


« Posted 2005-04-22 19:50:15 »

Hi,

In my program, each user has a JComboBox. However, the number of users varies depending on how many users are specified at the start of the game.

As a result I decided to make an array of JComboBoxes:
private JComboBox[] characterBox;

Then, later I specify the size of the JComboBox:
characterBox=new JComboBox[size];

I have no problem creating the JComboBoxes or adding them to the JPanel.

The problem occurs when I try to make actionlisteners. The following code is in a for loop that runs the number of users in the game.

characterBox
  • .addActionListener(
         
                        new ActionListener()
                        {
                                 public void actionPerformed( ActionEvent event )
                               {
                                              
                                         String picNum = (String)characterBox
  • .getSelectedItem();
                                         
                        
                                }
                             }
                         );//END Listener

    The problem is that the compiler tells me x must be final. However I can't have it final because I am looping the creating of the Listeners. I know the actionlistener code works but I can't figure out how to create all of the listeners in a for loop. Any thoughts? Thanks.
Offline digitprop

Junior Member





« Reply #1 - Posted 2005-04-24 10:49:38 »

I would parameterize the JComboBox into the constructor. Here is an example with an explicit class implementing ActionListener:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class MyActionListener implements ActionListener
{
   private JComboBox comboBox;

   public MyActionListener(JComboBox comboBox)
   {
      this.comboBox=comboBox;
   }

   public void actionPerformed(ActionEvent e)
   {
      String picNum=(String)comboBox.getSelectedItem();
      // ... do something ...
  }
}


The problem with your approach is that x is not known at compile-time, and whereas x is in the scope of the executed code at run-time, for the implicit ActionListener implementation it is not (as it will be created at compile-time, not at run-time). The compiler wants it to be final, because then it would be possible to get the actual integer value at compile-time.

Instead of having a reference to the JComboBox for each listener instance, you could also have a class member integer which is assigned the value of x; however, I would prefer the JComboBox as it makes it clear what the stored value is about.

M. Fischer . www.digitprop.com
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #2 - Posted 2005-04-24 13:38:41 »

Another (better) way (in my opinion) :

use jcombobox.putClientProperty(key, value) to store user information into each combobox. (for example an integer, a UserInfo object...) and retrieve it with getClientProperty(key)

write only one ActionListener, like this :

ActionListener comboListener = new ActionListener(){
 public void actionPerformed(ActionEvent e){
    JComboBox cbx = (JComboBox) e.getSource();
    UserInfo ui = (UserInfo) cbx.getClientProperty("user.info");
    // do something depending on the user info
 }
};

add this listener to every user combos.

Lilian

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gjbeyl

Junior Newbie




Java games rock!


« Reply #3 - Posted 2005-05-10 23:13:09 »

You can cheat the compiler.  Grin

In the for loop:

final JComboBox finalCB = characterBox
  • ;
    finalCB.addActionListener(new ActionListener()  
    {
      public void actionPerformed( ActionEvent event )
      {          
         String picNum = (String)finalCB.getSelectedItem();  
      }
    });[/pre]
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (44 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!