Java Tips Weblog

  • Blog Stats

    • 2,473,329 hits
  • Categories

  • Archives

Disabled Glass Pane

Posted by Rob Camick on November 7, 2008

There may be times when you want to temporarily disable your frame. The easiest way to do this is to simply use setEnabled(false) on the frame. There are two things I don’t really like using this approach:

  • there is no visual indication the frame is disabled
  • the frame beeps at you when you click on it

Maybe you also require a different solution?

Looking into the architecture of the top level Swing containers we find a Glass Pane. The Glass Pane is like an invisible sheet of class that is painted over the Root Pane of a top level container. Basically, the Glass Pane covers the menu bar and the content pane of the frame. By providing our own custom implementation of a Glass Pane we can get around the problems listed above.

The DisabledGlassPane class will get you started with a simple example of how this might be done. First of all it creates its own listeners to intercept all the mouse and key events when the glass pane is visible. Then it paints its own background. The trick to painting its background is to use a Color object with transparency. This will give the glass pane its disabled look. There are only two methods to use in this class:

  • activate – makes the glass pane visible. You can specify some text to be displayed in the middle of the pane
  • deactivate – makes the glass pane invisible again.

It is likely that you will want to activate the glass pane from within the ActionListener of a button. Using code like the following your frame will look like the frame in the image below:

DisabledGlassPane glassPane = new DisabledGlassPane();
JRootPane rootPane = SwingUtilities.getRootPane(...);
rootPane.setGlassPane( glassPane );
glassPane.activate("Please Wait...");


Although you can’t tell from the above image, the cursor will be changed to the wait cursor. You can use the setBackground(…) method of the glass pane to change the disabled effect. Using the setForeground(…) method will change the text color of the label.

Get The Code

Related Reading

How to Use Root Panes
Java API: javax.swing.SwingUtilities

9 Responses to “Disabled Glass Pane”

  1. Ixanos said


    Is there also a way to selectively allow some of the controls to function?
    for example, assume that we have a JFrame with a JMenuBar and ContentPane and a StatusBar (the South part of the application).

    Is it possible to allow the JGlassPane to only cover the JMenuBar and the ContentPane but not the StatusBar?


  2. Rob Camick said

    No, but you could change the painting so that it only paints over the menubar and center of the content pane. You would then need to change the event handling code to catch any event generated over the status bar and forward the event to the component under the glass pane. The Swing tutorial on How to Use Root Panes has a working example of this.

  3. Sawa said

    Good work !(Y)

  4. Prajin said

    This is tip is good, I have a feeling it will help me in my work.

    – Thanks a lot.

  5. Anonymous said

    Thanks. This helped me in disabling /enabling frame without using OptionPane.

  6. dARKpRINCE said

    I tried using this by setting the glass pane of the frame of my Swing application. It didn’t work. My frame has several panels on it. I tried setting the glass pane of those Jpanels to but without any effect. The cursor doesn’t change and click events are still caught and executed.

  7. Nemo Sygsky said

    Very interesting idea and realization!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: