Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add elements to Combobox

 
E johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
E johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic