Results 1 to 6 of 6
- 07-24-2011, 12:19 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
problem using vectors and jcomboboxes
I am working on a program that integrates with mysql.
I am querying the database and using a vector to populate a JcomboBox with the results . This is done as the GUI gets built and is working fine. Based on the item chosen in the first JcomboBox I want to popultate the second JcomboBox. I am using an action listener on the first JcomboBox, calling another class, passing the item selected in the first box, doing the query and returning the result as a vector.
As I used a vector in the first case I thought I could just call
box2.addItem(vector2);
My problem is that I get the result of the query displayed as a single line (one entry) enclosed by []
I am puzzled because JComboBox Box1 = new JComboBox(vector1); works to set up a JcomboBox at intitialisation but I cannot add a vector in the same way to an empty box after it has been made visible.
I can input single lines using box2.addItem("new line etc"); but I want to add a list as I have done successfully using a vector like in box 1.
Could someone give me a hint as to what I am doing wrong?
Thank you
Paul
- 07-24-2011, 12:22 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Box 1 uses a constructor. The addItem(...) is adding the string representation of the vector. You can use a loop to add each item of the vector to the jcombobox.
- 07-24-2011, 01:22 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You can call it an inconsistency (or omission) in the API for the JComboBox class but the addItem( ... ) method just adds one single item to the list (as its name implies); you can't add all elements of a Vector to a JComboBox with just one method call. You have to call that method over and over again for each element in the Vector; something like this:
Java Code:for (Object item: yourVector) box2.addItem(item);
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-24-2011, 01:30 PM #4
I'd consider it better style to construct a new model and set it to the combo.
Java Code:comboBox.setModel(new DefaultComboBoxModel(vector));
- 07-24-2011, 01:32 PM #5
- 07-24-2011, 03:33 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Problem with Vectors
By travist120 in forum Threads and SynchronizationReplies: 4Last Post: 04-04-2011, 10:02 AM -
Vectors Problem
By dashwall in forum New To JavaReplies: 9Last Post: 01-04-2010, 11:16 PM -
problem with JComboBoxes?
By swayam07 in forum New To JavaReplies: 4Last Post: 08-27-2008, 05:41 AM -
problem with Vectors and getTotal() function
By java_fun2007 in forum New To JavaReplies: 2Last Post: 11-23-2007, 01:55 PM -
Problem with vectors in java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:56 AM
Bookmarks