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 / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

JToggleButton event firing

Thread view: 
Redbeard - 08 May 2008 03:18 GMT
It appears that the JToggleButton fires the event to change its state
when the button is released rather than when it is pressed.

I'm using the JToggleButton as a "card" in a Memory Card game since it
holds two images - one that shows when the button is unselected, and
the other when it is selected..  Unselected represents a card that is
face down, while selected is face up.

My concern is that if you hold the mouse button down but don't release
it until you are over a different button, you can see the "selected"
icon without truly selecting the button.  When the mouse exits the
button, it reverts back to the unselected image.  The associated
ActionEvent never fires.

Obviously, in a Memory Card game, that is considered a "cheat".  What
i want is to change the behavior so the button gets selected when the
button is pressed.

I've tried getModel().setPressed(boolean),
getModel().setRollover(boolean), getModel().setArmed(boolean), but
none of those do what I want.  Nor have I found any other method that
seems appropriate.

Any ideas?
Mark Space - 08 May 2008 03:55 GMT
> It appears that the JToggleButton fires the event to change its state
> when the button is released rather than when it is pressed.

So it may not do what you want because JToggleButton is designed for
radio buttons and check boxes.  What you describe is desired behaviour
for those UI items.

Try going back to a regular old JButton.  You'll have to manaully set
the icon, but that doesn't sound like any more work than what you've
already tried.

What you want to do is use MouseEvents.  You may have to specifically
enable them.  See the Java docs:

<http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html>

MOUSE_RELEASED is what you want, I think.
Redbeard - 08 May 2008 04:25 GMT
> > It appears that the JToggleButton fires the event to change its state
> > when the button is released rather than when it is pressed.
[quoted text clipped - 13 lines]
>
> MOUSE_RELEASED is what you want, I think.

Thanks for the reply.  You are correct, MouseReleased would be the
method I'd have to implement in the MouseListener interface.  But
JButton doesn't have any analog for the setSelected or isSelected
methods found in JToggleButton, and I use those quite a bit.  I'd have
to do more than just implementing the MouseListener interface to use a
JButton.  Before I do that, I'll hold off a bit in case someone else
knows a way to modify the JToggleButton's behavior.
Knute Johnson - 08 May 2008 06:17 GMT
> It appears that the JToggleButton fires the event to change its state
> when the button is released rather than when it is pressed.
[quoted text clipped - 20 lines]
>
> Any ideas?

Just write your own button.  This is really easy with a JPanel and a
MouseListener.

Signature

Knute Johnson
email s/nospam/linux/

Redbeard - 08 May 2008 12:27 GMT
> Just write your own button.  This is really easy with a JPanel and a
> MouseListener.
[quoted text clipped - 3 lines]
> Knute Johnson
> email s/nospam/linux/
Thanks for the reply.

I considered that first, but a JPanel doesn't have an
addActionListener method, which is why I went with the JToggleButton.
Looks like I may have to rethink that.
Knute Johnson - 08 May 2008 16:43 GMT
>> Just write your own button.  This is really easy with a JPanel and a
>> MouseListener.
[quoted text clipped - 8 lines]
> addActionListener method, which is why I went with the JToggleButton.
> Looks like I may have to rethink that.

Do you need keyboard input, is that why you want an ActionListener?  Why
don't you explain completely what you want your button to do?

Signature

Knute Johnson
email s/nospam/linux/

Redbeard - 11 May 2008 04:46 GMT
On May 8, 10:43 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
<snip>
> Do you need keyboard input, is that why you want an ActionListener?
No, I don't need keyboard input.  I just envisioned it reacting like a
button, and figured that it was easier to subclass JToggleButton than
reinvent the wheel.  Of course, when I decided to subclass
JToggleButtion, I wasn't aware of the behavior that prompted my
original post.  I discovered that by accident.

> Why
> don't you explain completely what you want your button to do?
I did.  It represents a "card" in the Memory game - sometimes called
'Concentration' after the TV show based on the Memory game.  When
someone clicks on the "card" it is "turned over" to reveal the face.
Since JToggleButton already had the ability to hold two images - one
which could be the back of the card and the other the fact - it seemed
like a obvious choice.  But as I said earlier, I wasn't aware of the
behavior that prompted this thread.

Since nobody else has chimed in with a way to override that behavior,
I have to assume that nobody is aware of any way to do so.  (Which may
mean that there isn't any way.)  So it looks like I "roll my own" and
subclass JPanel.
John B. Matthews - 11 May 2008 17:48 GMT
In article
<cd26e5b0-3633-4c3f-934b-404d4edeb754@t54g2000hsg.googlegroups.com>,
[...]
> I did.  It represents a "card" in the Memory game - sometimes called
> 'Concentration' after the TV show based on the Memory game.  When
[quoted text clipped - 8 lines]
> mean that there isn't any way.)  So it looks like I "roll my own" and
> subclass JPanel.

I think it's the ToggleButtonModel that's responsible for the behavior.
Why not instantiate JToggleButton and set the model to an extension of
DefaultButtonModel that overrides isArmed().

John
Signature

John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Redbeard - 12 May 2008 02:58 GMT
> In article
>
[quoted text clipped - 22 lines]
> trashgod at gmail dot com
> home dot woh dot rr dot com slash jbmatthews

Sounds like an idea that is worth a try.  Thanks!
John B. Matthews - 12 May 2008 13:42 GMT
In article
<64bb689b-59ba-4f2a-8109-408cc671f0f6@34g2000hsh.googlegroups.com>,

> > In article
> > <cd26e5b0-3633-4c3f-934b-404d4edeb...@t54g2000hsg.googlegroups.com>,
[quoted text clipped - 8 lines]
>
> Sounds like an idea that is worth a try.  Thanks!

Sorry, I misspoke: set the model to an extension of ToggleButtonModel
that overrides isArmed(). I think you still want the "toggle" behavior,
just not the "armed" behavior. Also note: the button model fires both
ActionEvent (for pressed) and ItemEvent (for both selected and
deselected), as part of the "group" notion for check boxes and radio
buttons.

It might be cleaner to extend AbstractButton and DefaultButtonModel to
create, in effect, a JConcentrationButton. The custom model could
enforce rules like "no peeking" and "exposing a third card flips the
other two," etc.

John
Signature

John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Roedy Green - 11 May 2008 13:00 GMT
On Wed, 07 May 2008 22:17:56 -0700, Knute Johnson
<nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted
someone who said :

>Just write your own button.  This is really easy with a JPanel and a
>MouseListener.

The source code for all the button-like Components like in src.zip.
Most IDES will show the code to you at a click. You can see the basic
"trick".  You don't need any of the fancy UI LAF stuff.

IIRC  the event handling logic has its own interfaces and is
pluggable, so may not even need to create a whole new component.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.