2

I have a checkbox. I will obtain one value from a database to determine whether the checkbox can be edited or not. If this value is zero, the checkbox should not be selected. How do I achieve that in code? Please help me out here. This is my code:

String status = "0"; // (obtained from the database)
if(status)
{
    // should not be editable - can't be selected.
} else {
    // can be selected.
}
Suhas Pai
  • 45
  • 10
shree
  • 2,745
  • 7
  • 28
  • 35

2 Answers2

4

If this is REALLY what you want to do instead of using a JLabel with appropriate text and/or icon, you can create an action listener for the checkbox and have it call setSelected:

// the action listener for the checkbox
private void myCheckBoxActionPerformed(java.awt.event.ActionEvent evt)
{
    if (status.equals("0")
        myCheckBox.setSelected(false);
    else
        myCheckBox.setSelected(true);
}

To say the least, this isn't an elegant solution, but it does give the appearance that the checkbox isn't editable.

Steve
  • 41
  • 1
2

Use the setEnabled method for that.

Mat
  • 202,337
  • 40
  • 393
  • 406