Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Here’s a code snippet that shows these changes in bold:

public void init() {
    setLayout(new BorderLayout());

    add (new Label(“Hello, World!”), BorderLayout.NORTH);

    Panel theWestPanel = new Panel();
    fTextField = new TextField(“TextField”);
    theWestPanel.add(fTextField);
    fLabelForTextField = new Label(“Your text is TextField”);
    theWestPanel.add(fLabelForTextField);
    add(theWestPanel, BorderLayout.WEST);

    setBackground(java.awt.Color.red);
    fButton = new Button(“White”);
    fButton.setBackground(java.awt.Color.white);
    add(fButton, BorderLayout.CENTER);
    fCheckbox = new Checkbox(“Checkbox”);
    add(fCheckbox, BorderLayout.EAST);
    fDialog = OKDialog.makeDialog(“You clicked the checkbox!”);

    Panel theSouthPanel = new Panel();

    Choice theChoice = new Choice();
    theChoice.addItem(“Choice Item 1”);
    theChoice.addItem(“Choice Item 2”);
    theChoice.addItem(“Choice Item 3”);
    theSouthPanel.add(theChoice);
    fLabelForChoice = new Label(“You haven’t chosen anything”);
    theSouthPanel.add(fLabelForChoice);
    add(theSouthPanel, BorderLayout.SOUTH);

At some point you’ll change the layout and discover that your components are no longer visible. The reason is that you must use the two-parameter version of add() when you add components to a BorderLayout. Double-check the Constraint, too—the only valid Constraints are BorderLayout.NORTH, SOUTH, EAST, WEST, and CENTER. If you leave off the Constraint or mis-type it, you may not see your component.

Using CardLayout

Figure 38.11 shows how HelloPlus.java looks when you switch to CardLayout. Like BorderLayout, you need to do more than just call setLayout(). CardLayout shows one component at a time; you call next() and previous() to move from one card to another. In the design shown in Figure 38.11, there’s a BorderLayout for the application, then a CardLayout in a Panel in the “South” position to hold the contents. Two buttons are in a Panel in the “North” position; by clicking those buttons you can issue calls to next() and previous(). The code changes from HelloPlus are

public void init() {

    setLayout(new BorderLayout());
    Panel theControls = new Panel();
    Button thePreviousButton = new Button(“Previous”);
    theControls.add(thePreviousButton);
    Button theNextButton = new Button(“Next”);
    theControls.add(theNextButton);

    add(theControls, BorderLayout.NORTH);
    
    fContents = new Panel();
    fContents.setLayout(new CardLayout());
    
    fContents.add (new Label(“Hello, World!”), “Hello”);

    Panel theTextPanel = new Panel();
    fTextField = new TextField(“TextField”);
    theTextPanel.add(fTextField);
    fLabelForTextField = new Label(“Your text is TextField”);
    theTextPanel.add(fLabelForTextField);
    fContents.add(theTextPanel, “Text”);
    setBackground(java.awt.Color.red);
    fButton = new Button(“White”);
    fButton.setBackground(java.awt.Color.white);
    fContents.add(fButton, “Button”);

    fCheckbox = new Checkbox(“Checkbox”);
    fContents.add(fCheckbox, “Checkbox”);
    fDialog = OKDialog.makeDialog(“You clicked the checkbox!”);

    Panel theChoicePanel = new Panel();
    Choice theChoice = new Choice();
    theChoice.addItem(“Choice Item 1”);
    theChoice.addItem(“Choice Item 2”);
    theChoice.addItem(“Choice Item 3”);
    theChoicePanel.add(theChoice);
    fLabelForChoice = new Label(“You haven’t chosen anything”);
    theChoicePanel.add(fLabelForChoice);
    fContents.add(theChoicePanel, “Choice”);

    add(“ fContents, BorderLayout.SOUTH);

    theNextButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        CardLayout theLayout = (CardLayout) fContents.getLayout();
        theLayout.next(fContents);
    }});

    thePreviousButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        CardLayout theLayout = (CardLayout) fContents.getLayout();
        theLayout.previous(fContents);
    }});


FIGURE 38.11  You can use CardLayout to simulate a deck of index cards, with one card showing at a time.


NOTE:  In addition to next() and previous(), the CardLayout supports first(), last(), and show(). The latter enables you to display a card by name.

Using GridBagLayout

Although you can control such factors as alignment and horizontal and vertical gap in the other layout managers, the GridBagLayout gives you the ultimate in flexibility. At its simplest, GridBagLayout works much like a grid, except that it puts each component in a cell of its preferred size. The total area that a component occupies is called its “display area.” You specify suggestions—called GridBagConstraints—that further control how the layout will appear.

The GridBagConstraints class has a number of variables to control the placement of a component:

  gridx and gridy are the coordinates of the cell where the next component should be placed. (If the component occupies more than one cell, these coordinates are for the upper-left cell of the component.) The upper-left corner of the GridBagLayout is at 0, 0. The default value for both gridx and gridy is GridBagConstraints.RELATIVE, which for gridx means the cell to the right of the last component that was added; for gridy it means the cell just below the last component added.
  gridwidth and gridheight tell how many cells wide and how many cells tall a component should be. The default for both gridwidth and gridheight is 1. If you want this component to be the last one on a row, use GridBagConstraint.REMAINDER for the gridwidth. (Use this same value for gridheight if this component should be the last one in a column.) Use GridBagConstraint.RELATIVE if the component should be the next-to-last component in a row or column.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.