Results 1 to 2 of 2
  1. #1
    kerode is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default display object corresponding to JComboBox selection

    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

    Java Code:
    /* 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[names.getSelectedIndex()]);
                        }
       
        
        
        
         }
    }
    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.

    Any help would be appreciated, thank you.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    my ItemEvent listener does not set the label to the corresponding item in the object array data

    In itemStateChanged() why do you set the text of l1 100 times?

    Try adding a several System.out.,println() statements so that you can trace:

    * when itemStateChanged() is called
    * when setText() gets called
    * what the value of name's selected index and its value is when setText is called

    Beyond that you might make things a little clearer by constructing a SSCCE: ie something without deck or the other components which others can compile and run to see the problem. Also the variable names could be better chosen to aid clarity.

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2010, 09:46 AM
  2. Replies: 2
    Last Post: 12-10-2009, 02:58 PM
  3. Replies: 5
    Last Post: 04-25-2009, 09:00 PM
  4. Replies: 8
    Last Post: 12-29-2008, 08:12 AM
  5. Replies: 0
    Last Post: 06-28-2008, 10:26 PM

Posting Permissions

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