This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JComboBox Question (DefaultComboBoxModel) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JComboBox Question (DefaultComboBoxModel)" Watch "JComboBox Question (DefaultComboBoxModel)" New topic
Author

JComboBox Question (DefaultComboBoxModel)

Thomas Bigbee
Ranch Hand

Joined: Nov 29, 2001
Posts: 48
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

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JComboBox Question (DefaultComboBoxModel)
 
Similar Threads
recursive Method for ComboBox
Collections with JComboBox
External files
need a --Select One-- entry in JComboBox
Not able to read the value of a component on one JPanel from a different JPanel