This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

how to reflect changes of Data in a JComboBox?

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
JComboBox has a constructor that takes a Vector. this will be its dataModel.
Say I have created the JComboBox and given it a Vector. If dynmically i add new Data to the Vector, how would I get the JComboBox to update to reflect the changes in data add?
Any ideas?
thanks ,
Sarone
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JComboBox has a method "addItem(Object anObject)", but this will work only if you have a data model that recognizes changes. I believe that the default model doesn't recognize changes. So you have two choices:
1) Write you own class that implements "ComboBoxModel" and use it, or
2) replace the entire data model every time you change the underlying data. This is done via:

Obviously "myComboBox" is your JComboBox instance, and "myData" is your new Vector (or Array) of data.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!