Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 3233 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  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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!