简体   繁体   English

如何使复选框在Java中不可编辑?

[英]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.
}

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: 如果确实要执行此操作,而不是使用带有适当文本和/或图标的JLabel,则可以为复选框创建一个动作侦听器,并将其调用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. 至少可以说,这不是一个很好的解决方案,但是它确实使复选框不可编辑。

为此使用setEnabled方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM