• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections with JComboBox  RSS feed

 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JComboBox has constructors that can take a Vector or a ComboBoxModel.

When I pass a DefaultComboBoxModel to it, and change the contents of the DefaultComboBoxModel, the drop-down list changes as expected. No problem.

However, if I pass a Vector to it and change the contents of the Vector, the drop-down list goes blank. It is still there, because if I click on any point in the blank space, the most recent option is called and the list is then visible.

I have tried calling validate() on the JComboBox, but it makes no difference.

I want to experiment with generics, and cannot do so with DefaultComboBoxModel. Can anybody tell me a simple way to use Vectors in Swing without weird results???
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i posted an example in your last thread

http://www.coderanch.com/t/343126/GUI/java/JComboBox-Vector

this is the line that does it (after adding to the vector)
cbo.setModel(new DefaultComboBoxModel(vec));

in case you you think it should be done without using 'DefaultComboBoxModel',
here is the JComboBox's constructor
 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael - I hadn't understood last time that this was how it worked and that was the key line. So I just need to update the data model after the Vector is updated. Neat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!