checkbox issue
hai
how to make a checkbox uneditable but not disabled?.
i have a checkbox in my screen which is already checked.if i click it ,it should not be unchecked .
hai
how to make a checkbox uneditable but not disabled?.
i have a checkbox in my screen which is already checked.if i click it ,it should not be unchecked .
If your not doing any operation based on CheckBox state (is selected or deselected), the below code will go.
jCheckBox1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
jCheckBox1.setSelected(true);
}
});
else inside the itemStateChanged(...) method, u just take care about the
user input
check this simple solution
public class Sun extends JFrame implements ActionListener {
private JCheckBox cb;
public Sun() {
cb = new JCheckBox();
cb.setActionCommand("toggle");
this.getContentPane().add(cb);
cb.setSelected(true);
cb.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public void actionPerformed(ActionEvent ae) {
if(cb.getActionCommand().equals("toggle")) {
cb.setSelected(true);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Sun().setVisible(true);
}
}
Is there any specific reason why you dont want the checkbox to be disabled? Given that you don't want users to change its state?
You can hack it in many ways to do what you want but thats going to introduce problems in your code.
If you don't want users to edit it, make it disabled. There is no method in JCheck Box that allows you to make it enabled but yet uneditable.
> Is there any specific reason why you dont want the
> checkbox to be disabled? Given that you don't want
> users to change its state?
>
> You can hack it in many ways to do what you want but
> thats going to introduce problems in your code.
>
> If you don't want users to edit it, make it disabled.
> There is no method in JCheck Box that allows you to
> make it enabled but yet uneditable.
Maybe s/he doesn't like the gray color when a component is disabled. :)