You are adding an array to the first index of the combo box. Anytime you see that weird gobbledegook you are seeing the default toString message.

You can loop through the array and add each item.

Also, there may be something in the combo box class that let's you add an array, check the API.

04-06-2011, 03:59 PM

doWhile

The code posted adds the array as an item...instead, loop over the values in the array and add them individually
Edit: too late once again

04-06-2011, 05:05 PM

fm3c2007

There is nothing in the API...only Binding elements from a database/Table.

I'm trying to avoid writing unnecessary code. I can't believe that there isn't a method that takes an array and fills the combobox.

I found:

public void addItem(Object anObject)

Adds an item to the item list. This method works only if the JComboBox uses a mutable data model

Or:

public void addItem(Object anObject)
Adds an item to the item list. This method works only if the JComboBox uses a mutable data model

Thanks

04-06-2011, 05:18 PM

camickr

Quote:

String[] LotOfItems = {"item1", "item2"};

Use proper variable names. The first character should NOT be upper cased.

You can just replace the model:

Code:

comboBox.setModel( new DefaultComboBoxModel( items ) );

04-06-2011, 06:06 PM

fm3c2007

Thanks camickr. My problem is solved. :)

04-07-2011, 02:35 AM

sunde887

Please mark your thread solved with the thread tools at the top of the page if you are done.

Also, adding with a loop only adds 2 lines of code

Code:

for(int i = 0; i < x; i++){
comboBox.addItem(x[i]);
}

If you are trying to optimize away 2 lines of code you are over, and prematurely optimizing.

04-07-2011, 03:40 AM

camickr

Removing/adding items causes extra work. When you clear all the items an event is generated to tell the combo box the model has changed. Then every time you add an individual item another event is fired, again telling the combo box the model has changed. Is this overhead significant? No, but whats wrong with simple replacing the model so that only a single event is generated? The added bonus is that its only a single line of code.

04-07-2011, 04:34 AM

sunde887

I didn't mean to contest your point, just clarifying mine. Perhaps I have learned something here as well.