The add( ) method is inherited by JFrame from the AWT class Container.
By default, the content pane associated with a JFrame uses border layout. The version
of add( ) just shown adds the label to the center location. Other versions of add( ) enable
you to specify one of the border regions. When a component is added to the center, its size
is adjusted automatically to fit the size of the center.
Before continuing, an important historical point needs to be made. Prior to JDK 5, when
adding a component to the content pane, you could not invoke the add( ) method directly
on a JFrame instance. Instead, you needed to call add( ) on the content pane of the JFrame
object. The content pane can be obtained by calling getContentPane( ) on a JFrame instance.
The getContentPane( ) method is shown here:
Container getContentPane( )
It returns a Container reference to the content pane. The add( ) method was then called on
that reference to add a component to a content pane. Thus, in the past, you had to use the
following statement to add jlab to jfrm:
jfrm.getContentPane().add(jlab); // old-style
Here, getContentPane( ) first obtains a reference to content pane, and then add( ) adds the
component to the container linked to this pane. This same procedure was also required to
invoke remove( ) to remove a component and setLayout( ) to set the layout manager for the
content pane. You will see explicit calls to getContentPane( ) frequently throughout pre-5.0
code. Today, the use of getContentPane( ) is no longer necessary. You can simply call add( ),
remove( ), and setLayout( ) directly on JFrame because these methods have been changed
so that they operate on the content pane automatically.
The last statement in the SwingDemo constructor causes the window to become visible:
jfrm.setVisible(true);
The setVisible( ) method is inherited from the AWT Component class. If its argument is
true, the window will be displayed. Otherwise, it will be hidden. By default, a JFrame is
invisible, so setVisible(true) must be called to show it.
Inside main( ), a SwingDemo object is created, which causes the window and the label to
be displayed. Notice that the SwingDemo constructor is invoked using these lines of code:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo();
}
});
This sequence causes a SwingDemo object to be created on the event dispatching thread
rather than on the main thread of the application. Here's why. In general, Swing programs
are event-driven. For example, when a user interacts with a component, an event is
generated. An event is passed to the application by calling an event handler defined by the
application. However, the handler is executed on the event dispatching thread provided by
Swing and not on the main thread of the application. Thus, although event handlers are
defined by your program, they are called on a thread that was not created by your program.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home