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
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./* 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]); } } }
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.