Hello,
i can't manage to check a JCheckBoxMenuItem. Here is the important code (mostly generated by eclipse 3.0.1):
private javax.swing.JCheckBoxMenuItem getPointsCheckBoxMenuItem() {
if (pointsCheckBoxMenuItem == null) {
pointsCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
pointsCheckBoxMenuItem.setText("Afficher les points");
pointsCheckBoxMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//pointsCheckBoxMenuItem.getModel().isSelected()
System.err.println("pointsCheckBoxMenuItem.getState() = " + pointsCheckBoxMenuItem.getState());
System.err.println("pointsCheckBoxMenuItem.isSelected() = " + pointsCheckBoxMenuItem.isSelected());
if (pointsCheckBoxMenuItem.getModel().isSelected()) {
System.err.println("hide");
pointsCheckBoxMenuItem.setSelected(false);
getPointsJScrollPane().setVisible(false);
validate();
repaint();
} else {
System.err.println("show");
pointsCheckBoxMenuItem.setSelected(true);
getPointsJScrollPane().setVisible(true);
validate();
repaint();
}
}
});
pointsCheckBoxMenuItem.setSelected(false);
}
return pointsCheckBoxMenuItem;
}
the OUTPUT:
pointsCheckBoxMenuItem.getState() = true
pointsCheckBoxMenuItem.isSelected() = true
hide
pointsCheckBoxMenuItem.getState() = true
pointsCheckBoxMenuItem.isSelected() = true
hide
getState() is deprecated, it's just for testing
The checkbox is always detected as selected, while it is painted as it's not (in the menu, the item is NOT checked), and i can't select it!!
Any idea, plz?
TIA
Roland - 17 Feb 2005 14:47 GMT
> Hello,
>
[quoted text clipped - 45 lines]
>
> TIA
Your code resets the selected state immediately after it has been
selected: don't use
pointsCheckBoxMenuItem.setSelected(false)
in the actionPerformed method (and neither with arg true). The GUI
component takes care of updating the selected state itself.

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Mr Smith - 17 Feb 2005 15:28 GMT