All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.launch4j.formimpl.GlassPane Maven / Gradle / Ivy

Go to download

Cross-platform Java executable wrapper for creating lightweight Windows native EXEs. Provides advanced JRE search, application startup configuration and better user experience.

There is a newer version: 3.50
Show newest version
package net.sf.launch4j.formimpl;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;

/**
 * This is the glass pane class that intercepts screen interactions during
 * system busy states.
 * 
 * Based on JavaWorld article by Yexin Chen.
 */
public class GlassPane extends JComponent implements AWTEventListener {
	private final Window _window;

	public GlassPane(Window w) {
		_window = w;
		addMouseListener(new MouseAdapter() {});
		addKeyListener(new KeyAdapter() {});
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	}

	/**
	 * Receives all key events in the AWT and processes the ones that originated
	 * from the current window with the glass pane.
	 * 
	 * @param event
	 *            the AWTEvent that was fired
	 */
	public void eventDispatched(AWTEvent event) {
		Object source = event.getSource();
		if (event instanceof KeyEvent
				&& source instanceof Component) {
			/*
			 * If the event originated from the window w/glass pane,
			 * consume the event.
			 */
			if ((SwingUtilities.windowForComponent((Component) source) == _window)) {
				((KeyEvent) event).consume();
			}
		}
	}

	/**
	 * Sets the glass pane as visible or invisible. The mouse cursor will be set
	 * accordingly.
	 */
	public void setVisible(boolean visible) {
		if (visible) {
			// Start receiving all events and consume them if necessary
			Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
		} else {
			// Stop receiving all events
			Toolkit.getDefaultToolkit().removeAWTEventListener(this);
		}
		super.setVisible(visible);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy