I found this blocking GlassPane class on the web and I'm curious to know if some of you see any problem with it.
public final class BlockingGlassPane extends JComponent implements AWTEventListener {
// Events will be consumed for this window.
private Window parentWindow;
// Focus will be returned to this component.
private Component lastFocusOwner;
private final Toolkit toolkit;
public BlockingGlassPane() {
super();
setOpaque(false);
addMouseListener(new MouseAdapter() {
});
addKeyListener(new KeyAdapter() {
});
setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent anInput) {
return false;
}
});
toolkit = Toolkit.getDefaultToolkit();
}
@Override
public void setVisible(boolean b) {
if (b) {
if (parentWindow == null) {
parentWindow = SwingUtilities.windowForComponent(this);
}
Component focusOwner = parentWindow.getFocusOwner();
if (focusOwner != this) {
lastFocusOwner = focusOwner;
}
toolkit.addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
toolkit.addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
requestFocus();
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
} else {
toolkit.removeAWTEventListener(this);
if (lastFocusOwner != null) {
lastFocusOwner.requestFocus();
lastFocusOwner = null;
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
super.setVisible(b);
}
@SuppressWarnings("unchecked")
public void eventDispatched(AWTEvent e) {
Object source = e.getSource();
if (e instanceof EventObject && source instanceof Component) {
Component src = (Component) source;
EventObject ev = e;
if (SwingUtilities.windowForComponent(src) == parentWindow) {
try {
Class[] cls = {};
Object[] args = {};
ev.getClass().getMethod("consume", cls).invoke(ev, args);
} catch (Exception ex) {
// ex.printStackTrace();
}
}
}
}