Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Collections with JComboBox

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i posted an example in your last thread

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Liar, liar, pants on fire! refreshing plug:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic