Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

how do I veto a setSelected() on JCheckBox

Thread view: 
rob - 24 Feb 2006 15:15 GMT
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?
Vova Reznik - 24 Feb 2006 15:40 GMT
You'd better work with ButtonModel.
It has method setSelected(boolean)
You may put your business logic here
to define if you need to call super.

Default model for JCheckBox is JToggleButton.ToggleButtonModel

> Hello all, I am confused as to how to do this?
> I would like the change to veto a setSelected
[quoted text clipped - 27 lines]
>
> am I even approaching this right?
rob - 24 Feb 2006 16:42 GMT
thanks, i guess this would be much easier than messing
with vetos, maybe thats not even a valid way anyhow
Steve W. Jackson - 24 Feb 2006 16:14 GMT
> Hello all, I am confused as to how to do this?
> I would like the change to veto a setSelected
[quoted text clipped - 27 lines]
>
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Thomas A. Russ - 24 Feb 2006 23:07 GMT
> > 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
...
> 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.

That was what I thought of as well.
It has the additional beneficial property of being pro-active and
visible.  That means that the user isn't but in the situation of trying
to do something only to have the system then tell him it can't be done.
It is better to make this explicit and visible up front.  It will save
the user time and aggravation, so I would consider it to be a much
superior solution.  It isn't as if the user can enter something
arbitrary and unpredictable, so just have the code do the work in advance.

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Knute Johnson - 24 Feb 2006 17:20 GMT
> Hello all, I am confused as to how to do this?
> I would like the change to veto a setSelected
[quoted text clipped - 27 lines]
>
> am I even approaching this right?

Another option to consider is to disable the check box if it is not a
valid time to check it.

Signature

Knute Johnson
email s/nospam/knute/

rob - 24 Feb 2006 19:31 GMT
thanks all, i think the veto thing would not work because
"selected" is a property, but not a constrained property
so i may be out of luck. Disabled may be  an option but havn't thought
it out to much, just thought intercepting the potential property change
prior to it actually changing would do the trick, but maybe not.

thanks all


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.