Reputation: 127
I am having a list of items in a JCombo box, based on an another event I want to change the items in the list . I have a method like this
void changeChoices(Array[] foo)
{
JComboBox.removeAllItems();
for (int i=0;i < foo.length ; ++i)
JComboBox.addItem(foo[i]);
}
is this Valid??
Upvotes: 3
Views: 220
Reputation: 25950
Your code is not valid in terms of syntax.
foo
seems to be array of Array
objects, are you sure you are using
a combobox of array of Array
objects?
You have JComboBox.removeAllItems();
, this is not valid since
JComboBox
class doesn't have a static method called removeAllItems()
.
Inside for loop you use JComboBox.addItem(foo[i]);
,this is not
valid again due to a similar reason explained above, JComboBox
class doesn't have a static method called addItem()
.
A valid method could be like this one:
String[] oldValues = new String [5];
JComboBox comboBox = new JComboBox(oldValues);
public void changeChoices ( String [] newValues )
{
comboBox.removeAllItems();
for( int i = 0; i < newValues.length; i++ )
comboBox.addItem( newValues [ i ] );
}
Last but not least, if you are removing all values from a combobox and adding completely new ones, then you should handle their corresponding events in your code. You are possibly adding an ItemListener
to your combobox and implementing selected item events in itemStateChanged(ItemEvent event)
method. So you should implement what would happen if a newly added value is selected from your combobox. I hope this post is useful and makes sense.
Upvotes: 3