JComboBox problem, pse help!

Discussion in 'Java' started by Tobi Krausl, Nov 27, 2003.

  1. Tobi Krausl

    Tobi Krausl Guest

    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
     
    Tobi Krausl, Nov 27, 2003
    #1
    1. Advertising

  2. Tobi Krausl

    ak Guest

    "Tobi Krausl" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    ak, Nov 27, 2003
    #2
    1. Advertising

  3. Tobi Krausl

    Tobi Krausl Guest

    > 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
     
    Tobi Krausl, Nov 28, 2003
    #3
  4. Tobi Krausl

    Adam Guest

    > 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
     
    Adam, Nov 28, 2003
    #4
  5. Tobi Krausl

    Tobi Krausl Guest

    > 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, Nov 28, 2003
    #5
  6. Tobi Krausl

    ak Guest

    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.

    "Tobi Krausl" <> schrieb im Newsbeitrag
    news:...
    > > 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, Nov 28, 2003
    #6
  7. Tobi Krausl

    Tobi Krausl Guest

    > 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!
     
    Tobi Krausl, Dec 1, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Mullan

    JComboBox problem

    Allan Mullan, Apr 18, 2004, in forum: Java
    Replies:
    0
    Views:
    535
    Allan Mullan
    Apr 18, 2004
  2. salvatore
    Replies:
    0
    Views:
    493
    salvatore
    Jan 23, 2005
  3. __frank__

    Pse, help me to understand this code

    __frank__, Oct 28, 2005, in forum: C Programming
    Replies:
    18
    Views:
    2,098
    Neil Cerutti
    Oct 31, 2005
  4. Eduardo Dobay

    Using PSE under Win32

    Eduardo Dobay, Jun 23, 2007, in forum: Python
    Replies:
    2
    Views:
    314
    Eduardo Dobay
    Jun 26, 2007
  5. IgorB
    Replies:
    0
    Views:
    176
    IgorB
    Jun 5, 2006
Loading...

Share This Page