O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

298
|
Chapter 8, Rendering
#58 Block a Window Without a Modal Dialog
HACK
Before setting the component to visible, you should save the current mouse
image and then change it to a busy cursor. The cursor will only be in effect
over the component you call
setCursor( )
on, so only the blocked window
will get the busy signal. All other windows will still use their normal cur-
sors. When
unblock( )
is called, you can restore the cursor and hide the
WindowBlocker
component again.
Build a Test Process
To test the window blocker, you will need some sort of long-running pro-
cess. The class in Example 8-4,
LongProcess
, will start a 10-second count-
down when its
actionPerformed( )
method is called. It will also print the
current time left in a status label. Note that the call to
setText( )
on the sta-
tus label is inside of a
SwingUtilities.invokeLater( ) method. This makes
sure that
setText( ) is called from the Swing event thread, avoiding any
threading issues or deadlock.
Example 8-4. Filling up clock cycles
class LongProcess implements ActionListener, Runnable {
JLabel status;
WindowBlocker blocker;
public LongProcess(JLabel status, WindowBlocker blocker) {
this.blocker = blocker;
this.status = status;
}
public void actionPerformed(ActionEvent evt) {
blocker.block( );
new Thread(this).start( );
}
public void setText(final String text) {
SwingUtilities.invokeLater(new Runnable( ) {
public void run( ) {
status.setText(text);
}
});
}
public void run( ) {
for(int i=10; i>0; i--) {
// set the label
final String text = "("+i+") seconds left";
setText(text);

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required