Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (674)
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 2387 times)
0 Members and 1 Guest are viewing this topic.
Offline THEOWL

Senior Newbie

Java games rock!

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


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.

  • .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 Devvie

« 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:

class MyActionListener implements ActionListener
   private JComboBox comboBox;

   public MyActionListener(JComboBox 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 .
Offline c_lilian

Senior Devvie

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("");
    // do something depending on the user info

add this listener to every user combos.


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();  
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

basil_ (37 views)
2015-09-30 17:04:40

shadowstryker (19 views)
2015-09-29 15:55:06

TheSpaceHedgehog (24 views)
2015-09-29 01:58:48

GamerC4 (47 views)
2015-09-24 21:10:38

GamerC4 (67 views)
2015-09-24 21:09:48

htuy (22 views)
2015-09-24 04:57:24

htuy (32 views)
2015-09-24 04:56:35

htuy (23 views)
2015-09-24 04:56:09

htuy (23 views)
2015-09-24 04:47:33

Roquen (60 views)
2015-09-21 12:54:28
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 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‑
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!