shree
shree

Reputation: 2757

How to make a checkbox non-editable in Java?

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

Upvotes: 2

Views: 9973

Answers (2)

Steve
Steve

Reputation: 41

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.

Upvotes: 4

Mat
Mat

Reputation: 206775

Use the setEnabled method for that.

Upvotes: 2

Related Questions