How to temporarily disable event listeners in Swing?

You could use a common base class for your listeners and in it, have a static method to turn the listeners on or off: public abstract class BaseMouseListener implements ActionListener{ private static boolean active = true; public static void setActive(boolean active){ BaseMouseListener. Active = active; } protected abstract void doPerformAction(ActionEvent e); @Override public final void actionPerformed(ActionEvent e){ if(active){ doPerformAction(e); } } } Your listeners would have to implement doPerformAction() instead of actionPerformed() (This would be awful in an enterprise scenario, but in a single-VM model like in Swing, it should work just fine).

You could use a common base class for your listeners and in it, have a static method to turn the listeners on or off: public abstract class BaseMouseListener implements ActionListener{ private static boolean active = true; public static void setActive(boolean active){ BaseMouseListener. Active = active; } protected abstract void doPerformAction(ActionEvent e); @Override public final void actionPerformed(ActionEvent e){ if(active){ doPerformAction(e); } } } Your listeners would have to implement doPerformAction() instead of actionPerformed(). (This would be awful in an enterprise scenario, but in a single-VM model like in Swing, it should work just fine).

Normally I use a flag indicating API changes or user changes. For each of the listeners I would check the flag and if it's API changes just return.

One option that might work for you is just to put a glass pane up while loading in order to block events during that time: download.oracle.com/javase/tutorial/uisw....

This question looks like a similar problem and no satisfactory solution to it. I found this article helpful in critically examining my own designs. Is there a way to globally temporarily disable some component's listeners in Swing without removing and reattaching them?

Every JComponent maintains an EventListenerList, which is accessible to your subclass. If necessary, you can always operate on the list directly or build the desired behavior into your custom implementation of EventListener.

It is really^inf bad idea to temporarily disable event listeners in Swing. If your code is broken (or something else goes wrong), you may not be able to bring your application back to life - respond to user and other events. If you want to discard (respond but do nothing) to user events, you may use glass pane which can just ignore the events.

If your EDT is busy (which again you must avoid as much as possible) and you wanted to discard user action for that period, you may still use a glasspane and remove it using invokeLater to remove the pane after all the events have been responded (ignored by the glasspane) to. Full details including an SSCE can be found in this question.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions