0

The code I'm creating involves a JRadioButton and a JComboBox. I want the JComboBox to be enabled if the JRadioButton is selected and disabled if it's not selected or deselected. My problem is that the JComboBox won't be disabled if I deselect the JRadioButton. How can I do this? Here's my code

    LouisClub=new JComboBox();
    LouisClub.setEnabled(false);

    LouisClub.addItem("Writer");
    LouisClub.addItem("Photojournalist");
    LouisClub.addItem("Cartoonist");
    LouisClub.addItem("Layout Artist");

    Louis=new JRadioButton("The Louisian");

    Louis.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            LouisClub.setEnabled(true);
        }
    });
Harry Joy
  • 58,650
  • 30
  • 162
  • 207

2 Answers2

1

You should JCheckBox instead of JRadioButton for such things and then you need check for checkBox status in actionPerformed() method and based on that enable/disable comboBox. Something like

Louis=new JCheckBox();
Louis.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       LouisClub.setEnabled(((JCheckBox)e.getSource()).isSeleted());
    }
}

Also it might be good (Not sure) to use ChangeListener instead of ActionListener.

    Louis.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent ce) {
             LouisClub.setEnabled(((JCheckBox)ce.getSource()).isSeleted());
        }
    });
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
1

You should use an ItemListener instead of an ActionListener. Here is the code :

public class Toto extends JPanel {

    private JComboBox LouisClub;
    private JRadioButton Louis;

    /**
     * Create the panel.
     */
    public Toto() {
        LouisClub = new JComboBox();
        LouisClub.setEnabled(false);

        LouisClub.addItem("Writer");
        LouisClub.addItem("Photojournalist");
        LouisClub.addItem("Cartoonist");
        LouisClub.addItem("Layout Artist");

        Louis = new JRadioButton("The Louisian");

        Louis.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                boolean ok = e.getStateChange()==ItemEvent.SELECTED;
                LouisClub.setEnabled(ok);
            }
        });

        add(Louis);
        add(LouisClub);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new Toto());
        frame.pack();
        frame.setVisible(true);
    }
}

I have two more comments to make :

  1. Java developpers use to not use uppercase letters at the beginning of their variables (they prefer louisClub to LouisClub)
  2. My main method is working, but is not the best way to create a window (see the java tutorial for more information).

bye,

Jean-Marc

Jean-Marc Astesana
  • 1,242
  • 3
  • 16
  • 24
  • I'm just a student not a professional java developer so I don't understand why it's not okay for me to name my variables however I want to. – Andreah Sanchez Jan 22 '12 at 03:49
  • Your code will work, but, it will seem strange to "professional" developers. Everything is explained there : http://www.oracle.com/technetwork/java/codeconvtoc-136057.html – Jean-Marc Astesana Jan 22 '12 at 17:30