Java Reference
In-Depth Information
static void invokeLater(Runnable
obj
)
static void invokeAndWait(Runnable
obj
)
throws InterruptedException, InvocationTargetException
Ask the Expert
Q
:
You state that it is possible to add a component to the other regions of a border
layout by using an overloaded version of
add( )
. Can you explain?
A
:
As explained,
BorderLayout
implements a layout style that defines five locations to
which a component can be added. The first is the center. The other four are the sides
(i.e., borders), which are called north, south, east, and west. By default, when you
add a component to the content pane, you are adding the component to the center. To
specify one of the other locations, use this form of
add( )
:
void add(Component
comp
, Object
loc
)
Here,
comp
is the component to add and
loc
specifies the location to which it is added.
The
loc
value is typically one of the following:
In general,
BorderLayout
is most useful when you are creating a
JFrame
that contains
a centered component (which might be a group of components held within one of Swing's
lightweight containers) that has a header and/or footer component associated with it. In
other situations, one of Java's other layout managers will be more appropriate.
Here,
obj
is a
Runnable
object that will have its
run( )
method called by the event-dis-
patching thread. The difference between the two methods is that
invokeLater( )
returns im-
mediately, but
invokeAndWait( )
waits until
obj
.run( )
returns. You can use these methods
to call a method that constructs the GUI for your Swing application, or whenever you need
to modify the state of the GUI from code not executed by the event-dispatching thread. You