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

Block a Window Without a Modal Dialog #58
Chapter 8, Rendering
|
299
HACK
Putting It All Together
With your window blocking and long-process classes ready, you can put
them together in a simple program. The
main( ) method in Example 8-5 cre-
ates a text area, a button, and a status label. Then it makes a
WindowBlocker
and installs it with frame.setGlassPane( ). The LongProcess is added as an
action listener to the Start button, so when the user presses Start Processing,
the process will launch and call
WindowBlocker.block( ). Once the process
ends, it will call
WindowBlocker.unBlock( ), releasing the window.
// sleep for 1 second
try {
Thread.currentThread( ).sleep(1000);
} catch (Exception ex) {
}
}
// set the final status string
setText("Process Complete");
blocker.unBlock( );
}
}
Example 8-5. Testing the blocking window
public static void main(String[] args) {
JFrame frame = new JFrame("Blocking Window");
JTextArea jta = new JTextArea(10,40);
JScrollPane scroll = new JScrollPane(jta);
JButton start = new JButton("Start Processing");
JLabel status = new JLabel("status");
WindowBlocker blocker = new WindowBlocker( );
frame.setGlassPane(blocker);
start.addActionListener(new LongProcess(status,blocker));
Container comp = frame.getContentPane( );
comp.add("North", start);
comp.add("Center", scroll);
comp.add("South", status);
frame.pack( );
frame.show( );
}
Example 8-4. Filling up clock cycles (continued)

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