Coding_Master
Coding_Master

Reputation: 23

Java how to enable and disable Checkboxes depending if another checkbox has been tciked

What i want to do is lock a check box until atleast one check box has been selscted.

Heres what i have.

    JCheckBox cbox1 = new JCheckBox("box1");
    cbox1.setBounds(5, 0, 61, 34); // Bounds are just for example purposes 
    contentPane.add(cbox1);

    JCheckBox cbox2 = new JCheckBox("box2");
    cbox2.setBounds(5, 0, 61, 34);  // Bounds are just for example purposes 
    contentPane.add(cbox2);


    JCheckBox cbox3 = new JCheckBox("box3");
    cbox3.setBounds(5, 0, 61, 34);  // Bounds are just for example purposes 
    contentPane.add(cbox3);
    cbox3.setEnabled(false);

    JCheckBox cbox4 = new JCheckBox("box4");
    cbox4.setBounds(5, 0, 61, 34);  // Bounds are just for example purposes 
    contentPane.add(cbox4);
    cbox4.setEnabled(false);

Basically i only want box 3 and box 4 to be clickable if box 1 or 2 have been clicked.

i tried this:

    if(cbox1.isSelected()||cbox2.isSelected()){
        cbox3.setEnabled(true);
        cbox4.setEnabled(true);
    }

but that doesnt work. could someone please help explain why it doesnt work and show me a different method.

btw i tried this it also didnt work

    JCheckBox cbox4 = new JCheckBox("box4");
    cbox4.setBounds(5, 0, 61, 34);  // Bounds are just for example purposes 
    contentPane.add(cbox4);
    cbox4.setEnabled(cbox1.isSelected());

Thanks in advance :)

Upvotes: 0

Views: 6719

Answers (1)

basic
basic

Reputation: 3408

This is a super basic ballpark way to enable or disable the checkboxes. Its pretty simple, all you need to do is declare an ActionListener. In the actionlistener you can then enable or disable the checkboxes per the selection. Like I said this very basic and doesn't account for a secondary click or things of that nature. Yet it will start with the boxes disabled and enable them per your specs.

public Combos() {
    super("ComboBox Example");
    setSize(800, 800);
    setLocationRelativeTo(null);
    add(configureBoxes());
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    boxThree.setEnabled(false);
    boxFour.setEnabled(false);

      ActionListener al = (ActionEvent e) -> {
        if (e.getSource() == boxTwo || e.getSource() == boxOne) {
            if (boxThree.isEnabled() && boxFour.isEnabled()) {
                boxThree.setEnabled(false);
                boxFour.setEnabled(false);
            } else {
                boxThree.setEnabled(true);
                boxFour.setEnabled(true);
            }
        }
    };

    boxOne.addActionListener(al);
    boxTwo.addActionListener(al);
}

As stated you will need to customize this code to manage your different events such as box was deselected or a secondary box was selected but this should be ample to get you going.

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Upvotes: 2

Related Questions