The Glass Pane

JRootPane may seem a bit confusing at first glance. The important thing to remember is that in most cases, all you need to worry about is adding your component to the content pane and possibly setting a menu bar. As we noted earlier, the menu bar and content pane are part of the layered pane, which we’ll look at in detail in the next section. In this section, we’ll explain the other component contained by JRootPane: the “glass pane.”
 
The glass pane is a component that is laid out to fill the entire pane. By default, it is an instance of JPanel, but it can be replaced with any Component. JRootPane’s implementation of the addImpl( ) method ensures that the glass pane is the first component in the container, meaning that it will be painted last. In other words, the glass pane allows you to place components “above” any other components in the pane. Because of this, it generally makes sense for the glass pane to be nonopaque; otherwise, it will cover everything in the layered pane. It’s important to remember that when the layout of the JRootPane is performed, the placement of the contents of the glass pane will have no effect on the placement of the contents of the layered pane (and its content pane). Both sets of components are placed within the same component space, overlapping each other as necessary. It’s also important to realize that the components in the various panes are all equal when it comes to receiving input: mouse events are sent to any component in the JRootPane, whatever part of the pane it happens to be in.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// Show how a glass pane can be used to block mouse events.
public class GlassExample extends JFrame {
	JPanel glass = new JPanel(new GridLayout(0, 1));
	JProgressBar waiter = new JProgressBar(0, 100);
	Timer timer;

	public GlassExample() {
		super("GlassPane Demo");
		setSize(500, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Set up the glass pane with a little message and a progress bar.
		JPanel controlPane = new JPanel(new GridLayout(2, 1));
		controlPane.setOpaque(false);
		controlPane.add(new JLabel("Please wait..."));
		controlPane.add(waiter);
		glass.setOpaque(false);
		glass.add(new JLabel()); // Padding...
		glass.add(new JLabel());
		glass.add(controlPane);
		glass.add(new JLabel());
		glass.add(new JLabel());
		glass.addMouseListener(new MouseAdapter() {
		});
		glass.addMouseMotionListener(new MouseMotionAdapter() {
		});
		setGlassPane(glass);

		// Now set up a few buttons and images for the main application.
		JPanel mainPane = new JPanel();
		mainPane.setBackground(Color.white);
		JButton redB = new JButton("Red");
		JButton blueB = new JButton("Blue");
		JButton greenB = new JButton("Green");
		mainPane.add(redB);
		mainPane.add(greenB);
		mainPane.add(blueB);
		mainPane.add(new JLabel(new ImageIcon("dummyImage.gif")));

		// Attach the pop-up debugger to the main app buttons so you
		// see the effect of making a glass pane visible.
		PopupDebugger pd = new PopupDebugger(this);
		redB.addActionListener(pd);
		greenB.addActionListener(pd);
		blueB.addActionListener(pd);

		// And last but not least, our button to launch the glass pane
		JButton startB = new JButton("Start the big operation!");
		startB.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent A) {
				glass.setVisible(true);
				startTimer();
			}
		});

		Container contentPane = getContentPane();
		contentPane.add(mainPane, BorderLayout.CENTER);
		contentPane.add(startB, BorderLayout.SOUTH);
	}

	// A quick method to start up a 10-second timer and update the progress bar
	public void startTimer() {
		if (timer == null) {
			timer = new Timer(1000, new ActionListener() {
				int progress = 0;

				public void actionPerformed(ActionEvent A) {
					progress += 10;
					waiter.setValue(progress);

					// Once we hit 100%, remove the glass pane and reset the
					// progress bar
					// stuff.
					if (progress >= 100) {
						progress = 0;
						timer.stop();
						glass.setVisible(false);
						waiter.setValue(0);
					}
				}
			});
		}
		if (timer.isRunning()) {
			timer.stop();
		}
		timer.start();
	}

	// A graphical debugger that pops up whenever a button is pressed
	public class PopupDebugger implements ActionListener {
		private JFrame parent;

		public PopupDebugger(JFrame f) {
			parent = f;
		}

		public void actionPerformed(ActionEvent ae) {
			JOptionPane.showMessageDialog(parent, ae.getActionCommand());
		}
	}

	public static void main(String[] args) {
		GlassExample ge = new GlassExample();
		ge.setVisible(true);
	}
}