Java problem, disabled controls still fires events!
I'm new to Java, I'm coding on NetBeans. The problem is that whenever I disable a control i.e jmenu.setEnabled(false) it still fires events! holy crap! how is it! :P
How can I p开发者_StackOverflow社区revent it?
From the javadoc:
Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.
Note: Disabling a heavyweight container prevents all components in this container from receiving any input events. But disabling a lightweight container affects only this container.
You might want to check out disableEvents(long mask).
To fit in with the event model adopted by Swing, I think your best option is to just add a check of isEnabled() in the handlers that you don't want executed when the component is disabled.
Consider using javax.swing.Action
-controlled Swing components.
In this way, you can instead disable an Action directly with Action.setEnabled
. Its component(s) will adopt its state automatically. When disabled in this manner, the components won't receive MouseEvents.
See the docs on the constructor JButton(Action)
.
精彩评论