Results 1 to 3 of 3
  1. #1
    thayalan is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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:
    Java 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.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    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.

Similar Threads

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

Posting Permissions

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