296
|
Chapter 8, Rendering
#58 Block a Window Without a Modal Dialog
HACK
// create the right-click glass pane.
Component rc = new RightClickGlassPane(frame.getContentPane( ),popup);
// set as glasspane and make it visible
frame.setGlassPane(rc);
rc.setVisible(true);
// pack and show the frame
frame.pack( );
frame.setSize(400,200);
frame.show( );
}
While most of this is boilerplate, be sure to notice the call to rc.setVisible().
This is where the glass pane is turned on. If the glass pane is not visible, then
your code won’t ever receive events and work its magic.
This hack only demonstrates intercepting a mouse click, but it could be used
for any other type of event capturing, such as remapping one key to another,
recording mouse events, blocking mouse events
[Hack #58], or creating com-
pletely synthetic events to fool the program.
H A C K
#58
Block a Window Without a Modal Dialog Hack #58
Block the input in a single window during long operations without stopping
your entire application.
Since the dawn of GUIs, most toolkits have had the concept of a modal dia-
log box. This is a small window that restricts input to itself, blocking access
to the rest of the program (or entire operating system in some cases). Modal
windows often produce the desired effect, but sometimes you need a win-
dow that can block itself without blocking access to the whole application.
The most common use for such a window is a long running process, like ren-
dering frames of a movie or waiting for the network to respond. In this case,
you would like to let the user still interact with the rest of the application but
block the one window that represents the work in progress. Swing doesn’t
provide a modal window like this, but since when has that stopped us?
Blocking Basics
To block a window, you could disable the components within it, but then
you would need to recursively find each component and disable it manu-
ally. This is a big headache, and would make for a very ugly window (all
those grayed-out components). All you really want to do is capture all input
to the window and block that. Swing provides a great way to do this: the
glass pane. The glass pane sits on top of all other components in a window,
making it the perfect place to implement blocking behavior.
Block a Window Without a Modal Dialog #58
Chapter 8, Rendering
|
297
HACK
Since you want your glass pane to be transparent, it’s best to start with a
plain
JComponent that doesn’t draw anything. Example 8-3 defines a
WindowBlocker
class that extends
JComponent
and implements the
MouseInputListener
(a compound interface that combines the
MouseListener
and
MouseMotionListener
).
So far, the code is pretty simple. The
WindowBlocker catches all mouse events
and does nothing, sending the events to that great bit bucket in the sky.
When the user presses the mouse, the computer will beep, indicating that
the window is busy. The component still won’t actually receive events
because it is invisible by default. When you are ready to start blocking
events, you should make the component visible. That’s where the
block( )
method comes in:
private Cursor old_cursor;
public void block( ) {
old_cursor = getCursor( );
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
setVisible(true);
}
public void unBlock( ) {
setCursor(old_cursor);
setVisible(false);
}
Example 8-3. Listening for mouse events
public class WindowBlocker extends JComponent
implements MouseInputListener {
public WindowBlocker( ) {
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Toolkit.getDefaultToolkit().beep( );
}
public void mouseReleased(MouseEvent e) {
}

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.