Hi,
I've a question concerning JComboBox:
In my program a use a JComboBox which is initialized with a list of
values at program startup. During runtime, this JComboBox is editable
so that a user can enter his own values.
I want to do following: After the user entered a valued in the
textfield of the JComboBox and a button is pressed, there should be
checked whether the JComoBox already contains the entered value. If
so, fine! If not, JComboBox.addItem(...entered value...).
How can I check whether a JComboBox or its related model already
contains a certain value??
Obviously there's no method like JComboBox.contains(Object o) or
JComboBox.getModel().contains(Object o)?!
Thank you,
Tobi
ak - 27 Nov 2003 09:41 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> Obviously there's no method like JComboBox.contains(Object o) or
> JComboBox.getModel().contains(Object o)?!
see javax.swing.DefaultComboBoxModel
Tobi Krausl - 28 Nov 2003 07:45 GMT
> see javax.swing.DefaultComboBoxModel
Fine, but javax.swing.DefaultComboBoxModel doesn't contain an obvious
solution for my problem. There's no method like "boolean
contains(Object o)".
There must be an easier solution than getting the size of the
DefaultComboBoxModel and iterate over all items?!!
Tobi
Adam - 28 Nov 2003 07:52 GMT
> Fine, but javax.swing.DefaultComboBoxModel doesn't contain an obvious
> solution for my problem. There's no method like "boolean
> contains(Object o)".
> There must be an easier solution than getting the size of the
> DefaultComboBoxModel and iterate over all items?!!
BTW, if 'boolean contains(Object o)' existed
it would probably iterate over all items to find the match.
Adam
Tobi Krausl - 28 Nov 2003 14:08 GMT
> BTW, if 'boolean contains(Object o)' existed
> it would probably iterate over all items to find the match.
That's exactly what I would need. However, it seems that this method
doesn't exist anymore. I use SDK1.4.2
ak - 28 Nov 2003 16:49 GMT
we told you really everything what you need to write following:
public boolean contains(DefaultComboboxModel model, Object o) {
int size = model.getSize();
for(int i = 0; i < size; i++) {
Object obj = model.elementAt(i);
if(obj.equals(o)) {
return true;
}
}
return false;
}
--
____________
http://reader.imagero.com the best java image reader.
> > BTW, if 'boolean contains(Object o)' existed
> > it would probably iterate over all items to find the match.
>
> That's exactly what I would need. However, it seems that this method
> doesn't exist anymore. I use SDK1.4.2
Tobi Krausl - 01 Dec 2003 08:10 GMT
> we told you really everything what you need to write following:
>
[quoted text clipped - 8 lines]
> return false;
> }
Thank you very much!