Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

JComboBox problem, pse help!

Thread view: 
Tobi Krausl - 27 Nov 2003 08:50 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.