Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JComboBox selection not showing item from object array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JComboBox selection not showing item from object array

    Hello I am trying to make a program where there is an array of strings which is filled with text from a jtextfield and put into a combobox. There is also an array of objects which is filled with items from a jlist. When you pick a string from the jcombobox, I want it so that the corresponding objects are displayed in three labels below the combobox.

    Here is my code for the action events


    /* This is what my string and object arrays are declared as at the top of my source code
     
    private Object[]  data= new Object[100];
    private String[] person = new String[100];
     
    */
     
    public void actionPerformed( ActionEvent event ) {
     
      if ( event.getSource()== b2 )
      cardManager.show( deck, "c1" );
     
      else if ( event.getSource()== b3 )
      cardManager.show( deck, "c2" );
     
      else if ( event.getSource()== b4 )
      cardManager.show( deck, "c3" );
     
            else if ( event.getSource()== b1)
            {
                    person[custIndex] = t1.getText();
                    names.addItem(person[custIndex]);
                    Object data[] = table.getSelectedValues(); 
     
                    custIndex++;
     
     
            }
     
     
      }
     
     
      public void itemStateChanged( ItemEvent event) {
     
        if (event.getStateChange()== ItemEvent.SELECTED ){
     
     
                     for (ct= 0; ct<data.length; ct++){
                    l1.setText((String)data[ct]);
                        }
     
     
     
     
         }
    }
    names is my JComboBox, and adding the person string array to the combobox is working fine when I click b1, but my ItemEvent listener does not set the label to the corresponding item in the object array data. I am using only one label just to test if it works, but it seems that my object array has no value when it is called in itemlistener. I have added the itemlistener to my combobox. My JList is table.

    When I use a System.out.println() statement, it shows that the value of any item in the combobox is null, so I am not sure why my ItemListener is not setting it to the corresponding index of the object data array.

    It seems to be working when I put :

    for (ct= 0; ct<data.length; ct++){
    l1.setText((String)data[ct]);
    }

    into the actionlistener for the b1 button, but when I put this for loop in my ItemListener, it does nothing.


    Any help would be appreciated, thank you.
    Last edited by oper125; November 23rd, 2010 at 04:18 PM. Reason: fixed code


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: JComboBox selection not showing item from object array

    If you look over your code you will realise hat youhave deffined Data to be a local variable within b1 event, so as soon as the b1 event i over, you data object gets destroyed so it will not be picked up by you ItemListener. I'm going to assume you have a global variable called data too, which when you create the local variable data you mask. So remove this local variable declaration and it should work fine.

    Chris

Similar Threads

  1. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  2. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM
  3. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  4. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM