Click Here!
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


  fill tells the GridBagLayout what to do when a component is smaller than its display area. The default value, GridBagConstraint.NONE, causes the component size to remain unchanged. GridBagConstraint.HORIZONTAL causes the component to be widened to take up its whole display area horizontally while leaving its height unchanged. GridBagConstraint.VERTICAL causes the component to be stretched vertically while leaving the width unchanged. GridBagConstraint.BOTH causes the component to be stretched in both directions to completely fill its display area.
  ipadx and ipady tell the GridBagLayout how many pixels to add to the size of the component in the x and y direction. The pixels are added on both sides of the component, so an ipadx of 4 causes the size of a component to be increased by 4 on the left and also 4 on the right. Remember that the component size grows by twice the amount of padding because the padding is added to both sides. The default for both ipadx and ipady is 0.
  insets is an instance of an Insets class and indicates how much blank space to leave between the borders of a component and edges of its display area. The Insets class has separate values for the top, bottom, left, and right insets.
  anchor is used when a component is smaller than its display area. It indicates where the component should be placed within the display area. The default value is GridBagConstraint.CENTER, which indicates that the component should be in the center of the display area. The other values are all compass points: GridbagConstraints.NORTH, GridBagConstraints.NORTHEAST, GridBagConstraints.EAST, GridBagConstraints.SOUTHEAST, GridBagConstraints.SOUTH, GridBagConstraints.SOUTHWEST, GridBagConstraints.WEST, and GridBagConstraints.NORTHWEST. As with the BorderLayout class, NORTH indicates the top of the screen; EAST is to the right.
  weightx and weighty are used to set relative sizes of components. A component with a weightx of 2.0, for instance, takes up twice the horizontal space of a component with a weightx of 1.0. Because these values are relative, no difference exists between all components in a row having a weight of 1.0 or a weight of 3.0. You should assign a weight to at least one component in each direction; otherwise, the GridBagLayout squeezes your components toward the center of the container.

Figure 38.12 shows HelloPlus.java laid out using a GridBagLayout. The code that produced this layout is as follows:

public void init() {
    GridBagLayout theGridBag = new GridBagLayout();
    setLayout(theGridBag);
    GridBagConstraints theConstraints = 
      new GridBagConstraints();

    // have all components expand to their largest size
    theConstraints.fill = GridBagConstraints.BOTH;

    // set the first label to span a row
    theConstraints.gridwidth = GridBagConstraints.REMAINDER;
    theConstraints.weightx = 1.0;

    Label theHelloLabel = new Label(“Hello, World!”, java.awt.Label.CENTER);
    theGridBag.setConstraints(theHelloLabel, theConstraints);
    add(theHelloLabel);

    // the text field and its label are a row
    theConstraints.gridwidth = GridBagConstraints.RELATIVE;
    theConstraints.weightx = 1.0;
    fTextField = new TextField(“TextField”);
    theGridBag.setConstraints(fTextField, theConstraints);
    add(fTextField);
    fLabelForTextField = new Label(“Your text is TextField”);
    theConstraints.gridwidth = GridBagConstraints.REMAINDER;
    theConstraints.weightx = 0.0;
    theGridBag.setConstraints(fLabelForTextField, theConstraints);
    add(fLabelForTextField);

    // make the button double-height
    setBackground(java.awt.Color.red);
    theConstraints.gridwidth = 1;
    theConstraints.gridheight = 2;
    theConstraints.weightx = 0.0;
    theConstraints.weighty = 1.0;
    fButton = new Button(“White”);
    theGridBag.setConstraints(fButton, theConstraints);
    fButton.setBackground(java.awt.Color.white);
    add(fButton);

    // let the checkbox end its own row
    theConstraints.gridwidth = GridBagConstraints.REMAINDER;
    theConstraints.gridheight = 1;
    fCheckbox = new Checkbox(“Checkbox”);
    theConstraints.weightx = theConstraints.weighty = 0.0;
    theGridBag.setConstraints(fCheckbox, theConstraints);
    add(fCheckbox);
    fDialog = OKDialog.makeDialog(“You clicked the checkbox!”);

    // and the choice and corresponding label span another row
    Choice theChoice = new Choice();
    theChoice.addItem(“Choice Item 1”);
    theChoice.addItem(“Choice Item 2”);
    theChoice.addItem(“Choice Item 3”);
    theConstraints.gridwidth = GridBagConstraints.RELATIVE;
    theConstraints.weightx = 1.0;
    theGridBag.setConstraints(theChoice, theConstraints);
    add(theChoice);
    fLabelForChoice = new Label(“You haven’t chosen anything”);
    theConstraints.gridwidth = GridBagConstraints.REMAINDER;
    theConstraints.weightx = 0.0;
    theGridBag.setConstraints(fLabelForChoice, theConstraints);
    add(fLabelForChoice);


FIGURE 38.12  Use the data members of the GridBagConstraint to set up each row of a GridBagLayout.

As you can see, most of the changes have to do with manipulating the data members of the GridBagConstraint as we move from row to row and cell to cell.

Adding Menus to Frames

We first saw a frame in Listing 38.1. You can attach a MenuBar class to a frame to provide drop-down menu capabilities. To make a menu bar and add it to a frame, write

MenuBar myMenuBar = new MenuBar();
myFrame.setMenuBar( myMenuBar );

After you have a menu bar, you can add menus to it. The following code fragment creates a menu called “File” and adds it to the menu bar:

Menu fileMenu = new Menu( “File” );
myMenuBar.add( fileMenu );

Some windowing systems enable you to create menus that stay up after you release the mouse button. These are referred to as “tear-off” menus. You can specify that a menu is a tear-off menu when you create it:

// true indicates it can be torn off
Menu tearOffMenu = new Menu( “Tear Me Off”, true ); 

In addition to adding submenus, you will want to add menu items to your menus. Menu items are the parts of a menu the user actually selects. Menus are used to contain menu items as well as submenus. The File menu on many systems, for example, contains menu items such as New, Open, Save, and Save As. You can add menu items to a menu in two ways. You can add an item name by writing

// Add an “Open” option to the file menu
fileMenu.add( “Open” );


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.