PeakGen
PeakGen

Reputation: 23005

Issues with ItemListener attached to JComboBox

Please have a look at the following code

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;

    public class ComboIssue extends JFrame
    {
        private JRadioButton rOne, rTwo;
        private ButtonGroup group;

        private JComboBox combo;

        private JLabel label;

        public ComboIssue()
        {
            rOne = new JRadioButton("One");
            rOne.addActionListener(new ROneAction());
            rTwo = new JRadioButton("Two");
            rTwo.addActionListener(new RTwoAction());
            group = new ButtonGroup();
            group.add(rOne);
            group.add(rTwo);

            combo = new JComboBox();        
            combo.addItem("No Values");
            combo.addItemListener(new ComboAction());

            label = new JLabel("labellLabel");

            this.setLayout(new FlowLayout());
            this.add(rOne);
            this.add(rTwo);
            this.add(combo);
            this.add(label);


        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        private class ROneAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("One");
            }
        }

        private class RTwoAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("Two");
            }
        }

        private class ComboAction implements ItemListener
        {
            public void itemStateChanged(ItemEvent ie)
            {
                if(ie.getStateChange() == ItemEvent.SELECTED)
                {
                    label.setText("Selected");
                }
            }
        }

        public static void main(String[]args)
        {
            new ComboIssue();
        }



}

Here what I am expecting is,

  1. Select one radio button. It will replace value in combo box.
  2. Select a value from the combo box. Now the JLabel text will be set to "Selected"

But, that is not what is happening. Instead, the JLabel text get changed as soon as you select a radio button!!! Why is this? Please help!

Upvotes: 2

Views: 911

Answers (3)

vels4j
vels4j

Reputation: 11298

Here your need is done with small changes

private class ROneAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            label.setText("Nothing Selected");   
            combo.removeAllItems();
            combo.addItem("One");
        }
    }

      private class RTwoAction implements ActionListener
      {
        public void actionPerformed(ActionEvent ae)
        {
            label.setText("Nothing Selected");
            combo.removeAllItems();
            combo.addItem("Two");
        }
       }
        private class ROneAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("Select");
                combo.addItem("One");
            }
        }

        private class RTwoAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("Select");
                combo.addItem("Two");
            }
        }

        private class ComboAction implements ItemListener
        {
            public void itemStateChanged(ItemEvent ie)
            {
                if(ie.getItem().equals("Two"))
                {
                    label.setText("Two Selected");
                } else if(ie.getItem().equals("One") ) {
                    label.setText("One Selected");
                }
            }
        }

Upvotes: 1

joey rohan
joey rohan

Reputation: 3566

This is beacuse of ComboAction implements ItemListener. Are you not changing the value of combobox? When you are selecting the value of radio button?

UPDATE:

Well, there was a bit problem with your code.It changes the value of label, as you were having a ItemListener.So i have adopted PopupMenuListener which will shoot when the list becomes invisible.Works just fine what you want.

code:

     import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class ComboIssue extends JFrame
{
    private JRadioButton rOne, rTwo;
    private ButtonGroup group;

    private JComboBox combo;

    private JLabel label;

    public ComboIssue()
    {
        rOne = new JRadioButton("One");
        rOne.addActionListener(new ROneAction());
        rTwo = new JRadioButton("Two");
        rTwo.addActionListener(new RTwoAction());
        group = new ButtonGroup();
        group.add(rOne);
        group.add(rTwo);

        combo = new JComboBox();
        combo.addItem("No Values");
        combo.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
        public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
        }
        public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            jComboBox1PopupMenuWillBecomeInvisible(evt);
        }
        public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
        }
    });

        label = new JLabel("labellLabel");

        this.setLayout(new FlowLayout());
        this.add(rOne);
        this.add(rTwo);
        this.add(combo);
        this.add(label);


    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class ROneAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            combo.removeAllItems();
            combo.addItem("One");
        }
    }

    private class RTwoAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            combo.removeAllItems();
            combo.addItem("Two");
        }
    }

       private void jComboBox1PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
   label.setText("selected");

}

    public static void main(String[]args)
    {
        new ComboIssue();
    }



       }

Upvotes: 1

exexzian
exexzian

Reputation: 7890

its because of this code in your radio button's action listener combo.removeAllItems();

when you are clicking a radio button then before adding that particular radio button text into combobox you are removing all items and after that only item left in the JComboBox is the one added after clicking radio button which is by default selected which then calls your JComboBox'sitemStateChanged which then changes the text on JLabel

Upvotes: 1

Related Questions