1

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!

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • This is because when you add the first item to the JComboBox, it gets automatically selected. This in turns, triggers an ItemEvent. I am not sure of what you are trying to achieve here since your comboboxes only contain a single value. – Guillaume Polet Jan 14 '13 at 18:59

3 Answers3

1

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();
    }



       }
joey rohan
  • 3,505
  • 5
  • 33
  • 70
1

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

exexzian
  • 7,782
  • 6
  • 41
  • 52
1

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");
                }
            }
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • This will still change the text of Label, when a radio button is selected. – joey rohan Jan 14 '13 at 19:39
  • `ItemListener` will be fired when you add items to combobox.The OP wants nothing to be done with the label,when selected a radio button.It should change text when selected only `combobox`. Sorry if I am not getting your code,possible,Can you post a small EG as update for my clarification? – joey rohan Jan 15 '13 at 09:44
  • @joeyrohan code is in the question only I did changes wherever it needs. – vels4j Jan 15 '13 at 10:44
  • I mean what about `Combo's ItemListener` – joey rohan Jan 15 '13 at 14:42