Luca9984
Luca9984

Reputation: 151

The name of my checkbox can not be resolved

I have a problem with my checkbox. When I try to read the state of it the name of it can not be resolved.

JCheckBox checkbox1 = new JCheckBox("Test");
    checkbox1.setBounds(6, 59, 121, 23);
    frmTree.getContentPane().add(checkbox1);

public void Checkbox() {

if (checkbox1.isSelected()) {
Sytem.out.println("Selected");
        }}

Upvotes: 0

Views: 369

Answers (2)

laurence keith albano
laurence keith albano

Reputation: 1482

//add this
checkbox1.setSelected(true);

Change your Checkbox function to this code:

//Try using boolean in your condition
//Get the selection state of the checkbox
boolean selected = checkbox1.isSelected();
if (selected) {
    System.out.println("Selected.");
} else {
    System.out.println("Not Selected.");
}

frmTree.getContentPane().add(checkbox1);

Upvotes: 0

Alok
Alok

Reputation: 266

hey instead of "isSelected()" try using "isChecked()".

Upvotes: 1

Related Questions