Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2009, 10:00 PM
Member
 
Join Date: Feb 2009
Posts: 11
Rep Power: 0
thayalan is on a distinguished road
Default [SOLVED] JComboBox
Hi there,

My app has got an array of JComboBox named comboBox and they have actionListeners attached. The question is how do I determine the array index of the comboBox that fired the event?

I need to know so that I can transfer the chosen value in comboBox to a JTextField with an identical array index.


Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-26-2009, 10:06 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,449
Rep Power: 8
Fubarable is on a distinguished road
Default
You can get the reference to the currently selected JComboBox via the actionPerformed method's ActionEvent parameter by calling getSource() on it. Then you can iterate through the combo box array using this object to determine which array item is the selected combo box. For example:
Code:
  public void actionPerformed(ActionEvent e)
  {
    JComboBox selectedComboBox = (JComboBox)e.getSource();
    int comboIndex = -1;
    for (int i = 0; i < comboBox.length; i++)
    {
      if (comboBox[i] == selectedComboBox)
      {
        comboIndex = i;
      }
    }
    // do what needs to be done with comboIndex here
  }

Last edited by Fubarable; 04-26-2009 at 10:11 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2009, 03:14 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
Default
Oh, no! Fubarable's been promoted!

You can also use the "name" property of each of the components. It's not normally set, but you can use that to keep them straight without having to look through an array.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JComboBox pinks_70986 New To Java 2 02-19-2009 06:26 PM
I need help with JComboBox eva21 New To Java 1 11-28-2008 11:05 PM
JCombobox daniel50096230 NetBeans 1 09-21-2008 01:23 PM
JComboBox Fosters AWT / Swing 0 08-10-2008 02:22 PM
jcombobox Freddie AWT / Swing 4 05-11-2007 01:48 AM


All times are GMT +2. The time now is 07:33 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org