mardi 17 novembre 2015

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 :)




Aucun commentaire:

Enregistrer un commentaire