How to make a checkbox non-editable in Java?

shree picture shree · Oct 23, 2011 · Viewed 8.3k times · Source

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.
}

Answer

Steve picture Steve · Dec 12, 2012

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.