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 .

[175 byte] By [manohar.sa] at [2007-11-27 11:01:25]
# 1

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

nirgun_xyza at 2007-7-29 12:36:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

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);

}

}

AnanSmritia at 2007-7-29 12:36:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

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.

AshwineeJhaa at 2007-7-29 12:36:44 > top of Java-index,Desktop,Core GUI APIs...
# 4

> 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. :)

Yannixa at 2007-7-29 12:36:44 > top of Java-index,Desktop,Core GUI APIs...