JComboBox Listener
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: JComboBox Listener

  1. #1
    Join Date
    Feb 2000
    Location
    Le Mans - France
    Posts
    105

    JComboBox Listener

    Hi,

    I've got a JComboBox on which i've set a listener.
    The problem is that Java enter into the listener code whatever action is done on the combobox (state changed).
    I would like Java enter into this code only when the active value of the combo box is changed (and not
    during the selection, when the list is open).

    Thanks a lot.

    Sylvain

    Car devastator !

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JComboBox Listener

    You'll have to be a bit more precise:

    What kind of a listener did you add?
    What do you mean by the 'active value' of the combobox?
    Do you mean the selected item?
    Do you mean you want to trap when characters are typed into the editbox?

    Dave




    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2000
    Location
    Le Mans - France
    Posts
    105

    Re: JComboBox Listener

    Hi, thanks for answering. Here is my code:



    main(){
    ...
    combo.addActionListener(this) ;
    ...
    }



    ...

    public void actionPerformed(ActionEvent evt)
    {
    Object obj = evt.getSource();

    if(obj.equals(this.combo))
    {
    JComboBox combo = (JComboBox)obj;
    String tempo = combo.getActionCommand();
    System.out.println("# COMMAND = " + tempo);
    }
    ...
    }




    The string tempo is always the same message.
    I just want to trap the value when the user has selected one in the list, but only that. The fact is that
    the action listener execute the code even when the user type some characters in the editbox....so each time
    he type characters, code is executed....

    Thanks.

    Sylvain

    Car devastator !

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JComboBox Listener

    If you just want notification of changes of the selected item, add an ItemListener:comboBox.addItemListener(new ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent event) {
    int stateChange = event.getStateChange();
    if (stateChange == ItemEvent.DESELECTED) {
    System.out.println("Combo item deselected");
    }
    else if (stateChange == ItemEvent.SELECTED) {
    System.out.println("Combo item "+ comboBox.getSelectedIndex() + " selected");
    }
    }
    });

    Dave


    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Feb 2000
    Location
    Le Mans - France
    Posts
    105

    Re: JComboBox Listener

    OK, i will try.
    Thanks.

    Sylvain.

    Car devastator !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center