aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to add elements to Combobox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add elements to Combobox" Watch "How to add elements to Combobox" New topic
Author

How to add elements to Combobox

E johnson
Greenhorn

Joined: Apr 20, 2002
Posts: 4
Hello
I have a combobox where in initially i populate the combobox by passing a vector which contains nearly 10000 values.At a later stage i want to re-populate the combo box,but it does not.
1)vAllSwitch-- is the actual vector containig the data
2) initally i populate by
Vector vAllSwitchhd = new Vector();
vAllSwitchhd = (Vector)vAllSwitch.clone();
cbxMTSwitch = new JComboBox(vAllSwitchhd);

3)Vector vAllSwitch1 = new Vector();
vAllSwitch1 = (Vector)vAllSwitch.clone();
cbxMTSwitch = new JComboBox(vAllSwitch1);
after step 3 the data is not populated in combobox,but when i display the value of vector it displays.
anny suggestion pl
Regards,
Johnson
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Well, if I decifered your code correctly, it appears that you are cloning 1 vector and then placing that vector in the combobox two different times.
Now, my first question would be do the values of that vector change so that you know for sure it is not working?
Second question, Are you adding values to the combobox or are you completely replacing all the values with new values?
If you answer yes to the second question, here is what I would do.
I would do a cbxMTSwitch.removeAllItems() first, and then add your Vector back in.
However, if the first is true, I ask again how do you know it is not working? Sometimes in SWING or AWT you need to call the revalidate() or rePaint() method to actually see component changes.


GenRocket - A Test Data Generation Platform
E johnson
Greenhorn

Joined: Apr 20, 2002
Posts: 4
Hello,
Now as per your queries The value of vector do not change.I am able to display the vector values but when i try to add it does not add.
I had tried cbxMTSwitch.removeAllItems() in this case it removes but then cobbobox is empty.
My component do not cahnge but problem is how to add values to combobox second time.If i loop throgh the vector and add the value is added,but i want to add it in one go just pass the vector in constructor.
Regards,
E Johnson
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

of course removeAllElements makes your combobox empty. That is what it does. If you have your Combobox filled with items from a Vector, then you try and fill that vector again with the same elements, you are going to have two (2) of every element. You must remove all the elements before adding the elements back in.
E johnson
Greenhorn

Joined: Apr 20, 2002
Posts: 4
but my problem of why the elements are not getting added to combobox is not yet addresssed.
if i remove all item before adding then i get a blank combobox else nothing is added to conmo box the previous value is retained.
Johnson
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
10000 values in a JComboBox? Maybe it is time to rethink the design some. That might be a bit too much. Adding and removing elements from JComboBox is not a major thing. Have you went throught the Sun tutorial? Have you searched in this forum? What are you trying to accomplish that the combo box gets filled with 10000 elements?
E johnson
Greenhorn

Joined: Apr 20, 2002
Posts: 4
Hi,
No one is trying to adress the problem why i am not able to add elements into combobox.Once i am able to add but when trying to add next time it does not get added.
Design is proper, no question of changing it. if any one can suggest. Pl try to address the core problem.
johnson
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Did you call repaint() or validate() or revalidatge() on the ComboBox after you added the new items? Try that and see if you see the new items.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add elements to Combobox
 
Similar Threads
ComboBox Item Display
JComboBox in a JTable Cell
cannot find symbol
any idea about JComboBox Modal? with JDBC
Display value and Actual value in JComboBox