Exercise 3: Adding Components

In this exercise, you will learn how to select a component in the Visual Java palette and place it in the desired location in your application. Specifically, you will add a label and labeled bar in the first two rows of the grid. You will also edit their attributes.

When you add components to the grid, you are laying them out in relation to one another. You will see the actual spacing of how the components will look in your application when you preview the application in Exercise 5.

Adding a Text Label

  1. Click the Text label component in the Visual Java palette.
    As you move your pointer over a component in the palette, the name of the component appears in the lower-left corner of the browser.

  2. In the layout window, click the first cell in the first column.
    A label appears in the cell. Eight small squares, or handles, appear on the sides and corners of the label. These handles indicate the label is selected.

    If you place a component in the wrong cell, delete it as follows: Select the component by clicking outside the component but within the cell. Then click the Cut control on the Visual Java tool bar.

  3. Click a handle on the right side of the component and drag it across the three cells to the right.
    The component now spans all four cells in the first row.

  4. Make the label the length of the grid by clicking the resize control on the bottom of the layout window.
    The resize control is a black line between two small boxes. When you click on the control, arrow heads appear on the ends of the black line and the component's size is automatically increased to fill the containing cell.

  5. Back in Visual Java, type display in the Name field.

  6. In the Text field, type 0. and press Return or Enter.
    The text 0. now appears in center of the label.

  7. Click to open the Attribute Editor.

  8. Set alignment to right and background to white.

  9. Click OK.
    In the layout window, the label background changes to white and the text is right-aligned. If you are developing the GUI on either Windows 95 or Windows NT, a bug prevents colors from displaying correctly. You will not see a color change in the label background.

Adding a Labeled Bar

  1. Click the Labeled bar component at the bottom of the Visual Java palette.

  2. Click the first cell in the second row.
    A bar appears in the cell.

  3. Click a handle on the right side of the component and drag it across the three cells to the right.

  4. Click the Save control on the Visual Java tool bar.
    Your GUI should look like this:

Next lesson:

Exercise 4: Adding Multiple Components