This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JButton disabling mouse listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "JButton disabling mouse listeners" Watch "JButton disabling mouse listeners" New topic
Author

JButton disabling mouse listeners

Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
I have a JButton, and have attached a mouse listener to
detect the mouse presses.
I try to disable this button by the setEnabled() method,
which does color the button differently, but does not
prevent the Action from performing when the button is pressed.

Is there any way to disable the action attached to this button
instead of adding and removing listeners on a per-click basis?

Zak
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
What about in the actionPerformed, doing a check to see if the button is enabled or not and based on this you can continue with the action or just return out of the actionPerformed
eg.

public void actionPerformed(ActionEvent ae){
boolean enabled = myButton.isEnabled();
if(enabled == false) return;
}

Cheers,
rachel
Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
But this is only for action listeners,
but what about mouse listeners attached to a JButton?

Zak
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
It's a bizarre problem, because when I use setEnabled, it swiches the action listeners off as expected. Could you post some code?

Cheers,
Rachel
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Zak Nixon:
But this is only for action listeners,
but what about mouse listeners attached to a JButton?


Why do you attach one? I never found the need to do so.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton disabling mouse listeners
 
Similar Threads
HyperLink but not URL!!
Basic Questions regarding JButton
Adding more ActionListeners to buttons.
Best approach for CardLayout
Mouse Listener Problem