I began writing a new cool Swing demo today. Yet, before getting the real stuff done, I wanted to be able to easily toggle on and off one or several glass panes. The idea is similar to Exposé on MacOS X where you use functions keys to toggle effects. I quickly came up with a simple hack to wire the F12 key to my glass pane's visibility by using an
AWTEventListener. This wasn't good enough.
So I wrote
GlassPaneActivator, a smarter
AWTEventListener. You can wire one glass pane on every key. Whenever you press the associated key, GlassPaneActivator finds out in which frame you pressed it and changes the glass pane. To be really useful, the activator keeps a stack of added glass pane. Thus, if you set up two activators, one wired on the F11 key and the other one on the F12 key, you can add a “F11 glass pane”, then a “F12 glass pane”, then a “F11 one”, etc. When you press a key associated to a glass pane already on screen, this glass pane is removed and the next in the stack is shown. Very handy to quickly compare several glass panes at once.
In the following demo, you can use both F11 and F12 keys to stack up glass panes. One shows a rotating square and the other one shows a white fade in and fade out effect. Try using different sequences of keys.
Here is an example of how to set up two glass panes on two different keys:
Toolkit.getDefaultToolkit().addAWTEventListener( new GlassPaneActivator(FadingPanel.class, KeyEvent.VK_F12), AWTEvent.KEY_EVENT_MASK); Toolkit.getDefaultToolkit().addAWTEventListener( new GlassPaneActivator(RotatingPanel.class, KeyEvent.VK_F11), AWTEvent.KEY_EVENT_MASK);
You need to pass the glass pane's class you want to use to the activator. This is very handy but also limitated since you have no control on the instanciation of the glass panes. I think it would be a great idea to use an IoC container here. Anyway you should download the source and try out the demo to see what I'm talking about.
GlassPaneActivator has not really been tested but it should work with any window in your application. Just note that it contains a small bug when you toggles off the last glass pane. The original glass pane is put pack in the window but it is set visible. Its state should definitely be retained.
Oh and I won't tell you what the messages in the window's header and the Insight title mean. I should be able to show you the real stuff within a few days.