Re: how do I veto a setSelected() on JCheckBox



In article <1140794158.846456.212720@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"rob" <rfwalker@xxxxxxxxxxx> wrote:

Hello all, I am confused as to how to do this?
I would like the change to veto a setSelected
when the event happens but not sure how to go about this.
If I am in the actionPerformed of a JCheckBox, I guess
at that point it is to late to veto right? I would liek to check
some business logic before allowing the check to take place
by using contstrained properties.

I was thinking I would have define my own checkbox like

MyVetoableCheckBox extends JCheckBox
{
void public setSelected(booleand b) { setVetableSelected(b); }

void public setVetableSelected(boolean b) throws
ProvertyVetoException
{

if( some business logic tells me not to allow the check )
{
Boolean oldValue = isSelected ? Boolean.TRUE : Boolean.FALSE;
Boolean newValue = selected ? Boolean.TRUE : Boolean.FALSE;
fireVetoableChange("selected", oldValue, newValue);
}
else
{
super.setSelected(b);
}
}

am I even approaching this right?

JCheckBox inherits from JComponent, which has the method
addVetoableChangeListener, so it seems possible (though I haven't tested
it) that you could accomplish this by making your checkbox implement
VetoableChangeListener (or creating such internally) and calling that
method. Then the vetoableChange method will fire when any property of
the checkbox is going to be changed, passing a PropertyChangeEvent.

As I said, I haven't tried this with a checkbox. But I have done it
with a JInternalFrame. It implements the interface and adds itself as a
VetoableChangeListener. When vetoableChange is called, it checks to see
if the property being changed is its IS_CLOSED_PROPERTY value (see the
API Javadocs) and if it's being changed from Boolean.FALSE to
Boolean.TRUE. If this is the case, my logic checks to see if the
content warrants a save prompt. If the user says to cancel that close,
then I veto the change and the close never happens.

I don't see any properties in JCheckBox that immediately suggest
themselves as candidates, so it may not be possible to do this the way
I'm describing. But it seems worth a closer look. Otherwise, an
alternative approach would be in order, I think, since the concept you
outline above would necessarily let the user change the state and then
forcibly change it back.

On the other hand, you might also consider simply not enabling the
checkbox if your conditions won't allow its state to change. If it's
not enabled, a user can't very well click it to change its checked state.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
.



Relevant Pages

  • Re: how do I veto a setSelected() on JCheckBox
    ... Default model for JCheckBox is JToggleButton.ToggleButtonModel ... I would like the change to veto a setSelected ... Boolean oldValue = isSelected? ... Boolean newValue = selected? ...
    (comp.lang.java.gui)
  • Re: how do I veto a setSelected() on JCheckBox
    ... I would like the change to veto a setSelected ... checkbox if your conditions won't allow its state to change. ... a user can't very well click it to change its checked state. ... superior solution. ...
    (comp.lang.java.gui)
  • JTree JCheckBox
    ... I want to use a JTree in which every node contains a JCheckBox. ... problem is that the check boxes have to be independent components. ... That means whenever a node is selected the checkbox should not be ...
    (comp.lang.java.gui)
  • JCheckbox in a JTable
    ... checkbox in any of the rows, but I don't know how to let a JDialog ... appear once I click on a checkBox, without selecting the checkBox. ... JDialog appear, then I want the checkBox be checked only ... Now I obtain the JCheckBox selected just after I click on it and the ...
    (comp.lang.java.gui)
  • Re: Swing less functionality than AWT?
    ... JCheckBox, a JRadioButton or a JToggleButton. ... Checkbox uses getState ...
    (comp.lang.java.programmer)