Sun Java Solaris Communities My SDN Account Join SDN
 
New to Java Programming Center

New to Java

Programming Center

Java Platform Overview | Getting Started | Step-by-Step Programming
Learning Paths | References & Resources | Certification | Supplements




Contents
BACK<<Containers and Components | Layout Managers | NEXT>>Labels and Images

Which is step 2?

Setting a layout manager in the constructor is the second step in the creating the Welcome panel. The DiveLog class is the entry point for the Dive Log application and contains the only main method that this application needs. From there, all other classes you created are initialized, so no other main methods are needed.

However, a layout manager for each class is necessary.

More on Layout Managers

Exploring the AWT Layout Managers

Effective Layout Management

Lesson: Laying Out Components Within a Container

Layout Managers

Before building and adding components to a container, it's good to have some idea of how you want those components arranged. Real world objects, like the furniture in your home, are arranged and set in specific locations, according to which objects need to be close to one another and which objects can be spaced farther apart. For instance, it might not make sense to put your couch beside your television, but it would make sense to set the couch in front of the television with some space between the two objects.

Application objects also need to be arranged. Layout managers are special objects that position the objects you create and add to containers. Each container, such as JFrame and Jpanel have a layout manager associated with them.

The Swing tool set primarily consists of the layout classes provided by the Abstract Window Toolkit (AWT), which has five layout manager classes:

  • FlowLayout
  • GridLayout
  • BorderLayout
  • CardLayout
  • GridBagLayout

In addition, Swing has several layout managers, such as the box, scroll pane, overlay, and viewport layouts. Deciding which manager to use depends on how you want to arrange the components within a container.

The simplest layout manager is flow layout because it arranges components from left to right. There are limited uses for this layout. If you have only one or two components in your application, then flow layout might be sufficient.

Border Layout Manager

The border layout manager is an easy-to-use layout manager. It is popular because you can do so much or so little with it.

Five components may be added to the border layout manager and arranged on any of the four borders and in the center. This pattern is similar to geographical locations indicated by North, South, East, West, and Center in the image below:


Dive Log containers with Border layout showing all five regions

If the application is resized, the components are resized accordingly, and the arrangement is preserved and maintained by the layout manager. Each region is stretched to fill the container. Notice how North and South regions extend to the left and right edges. Also note that the West, Center, and East areas do not extend to the upper and lower edges.

Only five components may be added to the border layout, one in each region. You can overcome this limitation by adding another container, such as a JPanel, with its own layout within that container. For instance, you might add a JPanel to the South panel, and this new JPanel might also use the border layout. Then you can add five more components to the South panel. You can do this endlessly and make complex layouts with many different layout managers. You'll see an example of this technique later.

Contrary to placing layouts within layouts, you don't have to make use of all five regions of the border layout manager.


Border layout using only three regions

Like the illustration above, the Welcome class uses the border layout with only three regions occupied by objects.

To start, you need to set up the class constructor, and call the method to use the border layout manager for this Welcome container.


  1. Open the Welcome.java file in your text editor.
  2. Create a constructor for this class:
  3. public Welcome()
        {  // Opens constructor   
        } // Closes constructor
    
  4. Immediately following the opening curly brace of the constructor, call the setlayout method:
  5. setLayout(new BorderLayout());
    
  6. In addition, set the background color of this panel:
  7. setBackground(Color.white);
    
  8. Save the file.
Your code should look match this Welcome.java file.

The setLayout method comes from the Container class and is inherited by making this class a JPanel. The method sets a layout for the container by initializing a layout object that you specify as a method argument. In this case, you initialized an object of the BorderLayout class, which makes this Welcome container managed by a border layout manager. Instructions for adding objects to this container in the arrangement of a border layout is covered later.

At this point, you are ready to create the components that are later added to the panel and arranged by the layout manager.

You declared three variables at the beginning of the Welcome class. Within the constructor, you create the objects to assign to those variables:

  • A dive flag image with a title
  • A text area with a titled border
  • An image of a diver and fish
Which classes should you initialize for the image, text, and text area objects?
   A.  Create JLabel, ImageIcon, and JTextArea classes.
   B.   Declare JText, JGIF, and JTextArea.
   C.   Neither.