JComboBox problem, pse help!

T

Tobi Krausl

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
 
A

ak

Tobi Krausl said:
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)?!

see javax.swing.DefaultComboBoxModel
 
T

Tobi Krausl

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
 
A

Adam

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
 
T

Tobi Krausl

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
 
A

ak

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;
}
 
T

Tobi Krausl

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;
}

Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top