Home : Course Map : Chapter 7 : Java :
Layout Managers - Part 2
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The following applets demonstrate five more tools for arranging components. Follow the links in the resources section for more information and details on these classes.

BoxLayout

BoxLayout arranges components sequentially like the FlowLayout manager but it will set them either horizontally or vertically as instructed. Unlike FlowLayout, however, it will not continue the components on the next line or column when there is insufficient room. Components out of range will not be shown.

BoxLayoutApplet.java
  

import javax.swing.*;
import java.awt.*;

/** Demo of BoxLayout **/
public class BoxLayoutApplet extends JApplet
{
  public void init () {

    Container content_pane = getContentPane ();

    // Create an instance of BoxPanel that uses a
    // a Box for layout.
    BoxPanel box_panel = new BoxPanel ();

    // And add one or more panels to the JApplet panel.
    content_pane.add (box_panel);
  } //init
} // class BoxLayoutApplet

 

/** Arrange five buttons using a BoxLayout. **/
class BoxPanel extends JPanel
{
  BoxPanel () {
    setLayout (new BoxLayout (this,BoxLayout.Y_AXIS));
    add (new JButton ("One") );
    add (new JButton ("Two") );
    add (new JButton ("Three") );
    add (new JButton ("Four") );
    add (new JButton ("Five") );
  } // ctor

} // class BoxPanel

 

Box Container

The Box is a container like JPanel except that it uses BoxLayout as a default instead of the FlowLayout used by JPanel. In addition, Box provides for three special invisible elements that insert spacing between components. You can create a Box object with its constructor or you can use two static methods to produce Box instances with either horizontal or vertical alignment:

  Box horizontalBox = Box.createHorizontalBox();
  Box verticalBox = Box.createVerticalBox();

 

BoxApplet.java
  

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**  Demo of using a Box to alayout the components. **/
public class BoxApplet extends JApplet
{
  public void init () {
    Container content_pane = getContentPane ();

    // Create a Box with horizontal alignment
    Box box = Box.createHorizontalBox ();

    // Add the components to the Box
    box.add (new JButton ("One") );
    box.add (new JButton ("Two") );
    box.add (new JButton ("Three") );
    box.add (new JButton ("Four") );
    box.add (new JButton ("Five") );

    // And add the Box to the JApplet panel.
    content_pane.add (box);
  } // init

} // class BoxApplet

 

Box Container with Glue & Struts

A Box also provides the following elements to specify the spacing between components:

  • Glue - surplus space placed in between components or between a component and its container's side
  • Strut - a fixed width or height spacing for horizontal or vertical alignments
  • RigidArea - both width and height dimensions are fixed values
BoxSpacingApplet.java
  

import javax.swing.*;
import java.awt.*;

/** Demo of Box with glue and struts. **/
public class BoxSpacingApplet extends JApplet
{
  public void init () {
    Container content_pane = getContentPane ();

    // Create a Box with horizontal alignment.
    Box box = Box.createHorizontalBox ();

    // Add the buttons plus spacing components.
    box.add (Box.createHorizontalGlue ());
    box.add (new JButton ("One") );
    box.add (Box.createHorizontalStrut (5));
    box.add (new JButton ("Two") );
    box.add (Box.createHorizontalStrut (5));
    box.add (new JButton ("Three") );
    box.add (Box.createHorizontalGlue ());
    box.add (new JButton ("Four") );
    box.add (Box.createHorizontalGlue ());
    box.add (new JButton ("Five") );
    box.add (Box.createHorizontalGlue ());

    // And add box to the Applet's panel.
    content_pane.add (box);
  } // init

} // class BoxSpacingApplet

 

CardLayout

The CardLayout arranges components into a "vertical" stack where only the top component is visible at a given time. Use next() method to move to next underlying component

 

CardApplet.java
  

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** Demo of the CardLayout manager. **/
public class CardApplet extends JApplet
{
  public void init () {

    Container content_pane = getContentPane ();

    // Create an instance of CardPanel
    CardPanel card_panel = new CardPanel ();

    // And add the CardPanel to the applet's panel.
    content_pane.add (card_panel);
  } // init

} // class CardApplet


/** Stack three buttons using CardLayout. **/
class CardPanel extends JPanel
                   implements ActionListener
{
  CardLayout fCards;

  /** Constructor adds three buttons to the panel
    * and uses CardLayout.
   **/
  CardPanel () {

    fCards = new CardLayout ();
    setLayout (fCards);

    add ("one",   makeButton ("one") );
    add ("two",   makeButton ("two") );
    add ("three", makeButton ("three") );

  } // ctor

  /** Create a JButton and add this object
    * to its action listeners.
   **/
  JButton makeButton ( String name) {
    JButton b = new JButton ( name );
    b.addActionListener ( this );
    return b;
  } // makeButton

  /** Flip to the next card when a button pushed. **/
  public void actionPerformed (ActionEvent e) {
     fCards.next ( this);
  } // actionPerformed

} // class CardPanel

 

JTabbedPane

The JTabbedPane is a component rather than a layout manager but it provides an alternative to CardLayout for overlaying a set of components. It provides a set of tabbed pages in which each page can hold a component. Selecting the tab for a particular page will bring that page to the top. Using a container such as JPanel can, of course, hold many sub-components for a page.

 

TabbedApplet.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** Demo of the JTablePane component. **/
public class TabbedApplet extends JApplet
{
  public void init () {
    Container content_pane = getContentPane ();

    // Create an instance of our Tabs class.
    Tabs tabs = new Tabs (this);

    // And add it to the applet's panel.
    content_pane.add (tabs);
  } // init

} // class TabbedApplet


/** This JTabbedPane subclass holds three panes, each
  * with one button.
 **/
class Tabs extends JTabbedPane
           implements ActionListener {
  JApplet fApplet;

  /** Put a button on each of three pages.  **/
  Tabs (JApplet applet)  {
    fApplet = applet;
    add (makeButton ("1"), "One" );
    add (makeButton ("2"), "Two" );
    add (makeButton ("3"), "Three" );
  } // ctor


  /** Make a button here and add this object
    * to its action listeners list.
   **/
  JButton makeButton ( String name)  {
    JButton b = new JButton ( name );
    b.addActionListener ( this );
    return b;
  } // makeButton

  /** When button pushed, show message on the browser status bar.**/
  public void actionPerformed ( ActionEvent e)  {
     JButton but =  (JButton) (e.getSource ());
     String str = but.getText ();
     fApplet.showStatus ("Pushed button "+ str);
  } // actionPerformed

} // class Tabs

 

 

References & Web Resources

Latest update: Nov. 3, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.