Java Reference
In-Depth Information
public
public class
class
RunOnEdt
RunOnEdt
{
public
public static
throws
Exception
{
System
.
out
.
println
(
"RunOnEdt.main()"
);
SwingUtilities
.
invokeLater
(
new
static
void
void
main
(
String
[]
args
)
throws
new
Runnable
() {
public
public
void
void
run
() {
try
try
{
JOptionPane
.
showMessageDialog
(
null
null
,
"Hello Java"
);
}
catch
catch
(
Exception e
) {
e
.
printStackTrace
();
}
}
});
}
}
Some books and articles on Swing GUIs do not mention Swing's thread-safety issues, but
you can read about it on
Oracle's Java website
.
We will omit this code for brevity from most
of the simple demos herein
, but production code should use it.
Designing a Window Layout
Problem
The default layout isn't good enough.
Solution
Learn to deal with a layout manager.
Discussion
The container classes such as
Panel
have the capability to contain a series of components,
but you can arrange components in a window in many ways. Rather than clutter up each con-
tainer with a variety of different layout computations, the designers of the Java API used a
sensible design pattern to divide the labor. A layout manager is an object that performs the
classes (see
Table 14-1
), and Swing has a few more. Plus, as we'll see in
Program: Custom
AWT/Swing Layout Manager
,
it's not that big a deal to write your own!