Results 1 to 9 of 9
- 04-06-2011, 02:53 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Combobox dynamic load using String[]
Hi,
I'm using NetBeans 6.9.1 . And in a form i place a combobox and a Button. I would like to click the button and fill the combobox with values from a String [].
I tried:
but all i get in the combobox text is "[Ljava.lang.String;@1050169".Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String[] LotOfItems = {"item1", "item2"}; jComboBox1.removeAllItems(); jComboBox1.addItem(LotOfItems); }
Thanks
- 04-06-2011, 02:59 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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, 02:59 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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, 04:05 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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, 04:18 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Use proper variable names. The first character should NOT be upper cased.String[] LotOfItems = {"item1", "item2"};
You can just replace the model:
Java Code:comboBox.setModel( new DefaultComboBoxModel( items ) );
- 04-06-2011, 05:06 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Thanks camickr. My problem is solved. :)
- 04-07-2011, 01:35 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
If you are trying to optimize away 2 lines of code you are over, and prematurely optimizing.Java Code:for(int i = 0; i < x; i++){ comboBox.addItem(x[i]); }
- 04-07-2011, 02:40 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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, 03:34 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Dynamic String Array?
By ladykrimson in forum New To JavaReplies: 4Last Post: 10-13-2010, 04:37 AM -
String array from file to ComboBox
By cselic in forum AWT / SwingReplies: 3Last Post: 05-06-2010, 05:29 PM -
How to create dynamic string object???
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-12-2010, 02:35 AM -
[SOLVED] Dynamic String -> Date Handeling
By vagetoanima in forum Advanced JavaReplies: 2Last Post: 03-31-2009, 06:43 PM -
Add data to combobox in page load
By hussainzim in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 05-15-2008, 12:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks