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)

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.