Results 1 to 8 of 8

Thread: combobox

  1. #1
    dina is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Exclamation combobox

    Hi all, I've been trying to get this done but really don't know how to.

    I have a button action listener implementation inside a combobox itemlistener implementation.

    My problem is whenever i select a combobox event my button listener is activating.I want my button listener to be activated only once when i hit my button.

    when I select my editable combobox item for third time and i was hitting my button only this time but it is activating three times,where it supposed to be activate only once,which is not the case i needed.

    can anyone help me why this is happening and what to do to avoid this.
    Here is the code from my program.

    /**code**/
    import java.awt.event.ActionEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;

    public class Test extends JPanel implements ItemListener{
    JComboBox jc = new JComboBox();
    JButton button = new JButton("submit");
    public Test() {
    jc.addItem("France");
    jc.addItem("Germany");
    jc.addItem("Italy");
    jc.addItem("Japan");
    jc.addItemListener(this);
    add(jc);
    add(button);
    }

    public void itemStateChanged(ItemEvent ievent) {
    if (ievent.getStateChange() == ItemEvent.SELECTED) {
    button.setEnabled(true);
    String str = (String) jc.getSelectedItem();
    System.out.println("ComboBox "+str);

    button.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("submit")) {
    System.out.println("Sequences after pressing button");
    }
    }
    });//end button action listener
    }
    }//end Combobox itemlistener
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Test());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(200, 200);
    frame.setVisible(true);
    }

    }
    /**code**/

    Case1:
    when I select France from combobox,
    This is the output Iam getting:
    France
    whenI press the submit button
    output is:
    Sequences after pressing button

    case 2:
    next time when I select Italy from combobox,
    output is:
    Italy
    and after pressing submit button
    output is:
    Sequences after pressing button
    Sequences after pressing button

    case 3:
    If i select another item from combobox example Germany,
    output is:
    Germany
    and after pressing submit button
    output is:
    Sequences after pressing button
    Sequences after pressing button
    Sequences after pressing button

    conclusion:
    so in case 2 and case 3 submit button is activating twice and thrice respectively,which is not the case needed for me.
    I want the button should activate only once, since iam pressing the button only once every time.

    Thankyou,
    dina
    Last edited by dina; 05-07-2011 at 04:57 PM. Reason: added output data

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dina View Post
    when I select my editable combobox item for third time and i was hitting my button only this time but it is activating three times,where it supposed to be activate only once,which is not the case i needed.
    You know what? I don't understand that sentence at all, not even syntactically.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Post your Short, Self Contained, Correct Example that demonstrates the problem.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    1) Use the "Code" tags when you post code so the code retains its formatting.

    2) Seems to work fine for me. Nothing repeats 3 times.

  5. #5
    dina is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default can you clarify my doubt about combobox which i posted earlier

    Quote Originally Posted by camickr View Post
    1) Use the "Code" tags when you post code so the code retains its formatting.

    2) Seems to work fine for me. Nothing repeats 3 times.
    hello camickr,Thankyou for your kind suggestion to improve my way of posting question,As this is the first time iam posting question like this to get help from other members,your suggestions really need to me.
    I hope, now you understand my problem in my code, after adding output.
    Can you please clarify my doubt,why is it happening so and what to do to get desired output.

    Thankyou,
    dina

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Those are not code tags. When you post a qestion you see all kinds of buttons like "B" for bold, "I" for Italic. Well, search for the one that displays a tooltip of "Wrap [code] tags around selected text" and use that button.

    Don't add the ActionListener to the button in the itemStateChanged() method.

    Instead add the ActionListener to the button when you create the button.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't want to add an ActionListener from within a SelectionListener, since each time you select something, you add another ActionListener, and this would explain why the ActionListener is firing more than once -- there are several listeners being added. Instead change the state of your program, say by setting a boolean field and then have the button's action listener use that field to decide what it wants to do.

    edit: too slow!

  8. #8
    dina is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Those are not code tags. When you post a qestion you see all kinds of buttons like "B" for bold, "I" for Italic. Well, search for the one that displays a tooltip of "Wrap [code] tags around selected text" and use that button.

    Don't add the ActionListener to the button in the itemStateChanged() method.

    Instead add the ActionListener to the button when you create the button.
    Its working,when I removed action listeners from itemlisatener and added to the button where i created.:)Thankyou.

Similar Threads

  1. combobox
    By leader in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 09:30 PM
  2. Combobox Problem?
    By waqasahmed_03235001393 in forum Advanced Java
    Replies: 1
    Last Post: 07-27-2010, 10:56 AM
  3. Problem in combobox
    By santhosh_el in forum AWT / Swing
    Replies: 4
    Last Post: 04-03-2009, 04:01 AM
  4. Need Help combobox
    By kwink in forum AWT / Swing
    Replies: 3
    Last Post: 03-21-2009, 10:05 AM
  5. combobox
    By chandu.v in forum New To Java
    Replies: 2
    Last Post: 07-02-2008, 08:36 PM

Tags for this Thread

Posting Permissions

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