This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

JComboBox Question (DefaultComboBoxModel)

 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings - I am trying to utilize the method getIndexOf( Object anObject ) to see if a value is already in the ComboBox before trying to add a duplicate value. This method is implemented by the DefaultComboBoxModel - My question is this - how can I implement the DefaultComboBoxModel with a JComboBox?
Thank you
 
Rob Ross
Bartender
Posts: 2205
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a couple ways, but for what you want, the easiest is to just create your own instance of a DefaultComboBoxModel and pass that to your JComboBox's constructor...
JComboBox(ComboBoxModel aModel)

then when you want to retrieve the model, just cast it to the right class after calling getModel()...
DefaultComboBoxModel myModel = (DefaultComboBoxModel) myJComboBox.getModel()
Then you can call all the DefaultComboBoxModel's methods.

Good luck!
Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic