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
Preparing for User Input | NEXT>>Setting Up the Diver Data Pane

Download or Print -->

More on User Input Components

An Example of Using Each Text Component

Text Components

By now you should feel comfortable with objects and methods. You have initialized instances of predefined objects, such as panels and labels, and you have called methods on these objects using a variable, or identifier, and the dot operator. In addition, you have defined your own objects and methods to build GUI components.

In Building an Application, Part 2, the Welcome class introduced inheritance, illustrated how to display images and labels, and demonstrated how to arrange objects using a layout manager.

Part 3 reinforces these concepts, introduces the Diver class, and shows you how to:

  • Retrieve and display user input.
  • Understand encapsulation and access control.
  • Arrange nested layouts.
  • Understand event handling basics.
  • Use inner classes.

Getting Started

In Part 1, you created the DiveLog class with a constructor that builds the frame for the Dive Log application, a JMenu, and initializes a JTabbedPane object with six titled tabs. Each tab creates an object from a placeholder class. In Part 2, you designed the Welcome class, which appears on the Welcome pane.

For this part of the tutorial, you need one image and the Diver.java placeholder class that creates the Diver Data pane. You can use a different image than the one provided here, but to prevent problems with layout, make the image same size as the image provided.


  1. Save the following image to the divelog/images directory:
Or create an image of your own, but use the same pixel size as the one listed above.


Note: It's assumed you installed the JavaTM 2 Platform, Standard Edition on your system, and that you have completed Part 1 and Part 2 of the Building an Application tutorial.

Preparing for User Input

Click to enlarge
Completed Diver panel

Displaying images and labels is useful, but this aspect of an application is passive, only showing information. Most applications also require parts of an application to be active, accepting user input and doing something with the data received.

The Java API contains many types of predefined components you can use to collect user data. Here are a few:

  • Text fields
  • Text areas
  • Check boxes
  • Radio buttons
  • Drop-down menus
  • Buttons

When designing an application, consider the type of data to be entered by a user, and how to collect the information. A Dive Log generally contains the name of the diver. In addition, contact information is needed in case of emergency. Other personal data is valuable in a dive log as well, such as the diver's level of formal training.

The Diver Data pane accepts input so the diver can enter the following types of information:

Personal

  • Name
  • Address

Emergency Contact

  • Name
  • Relationship to that person
  • Phone number

Formal Training

  • List of various training levels

Remember the Java programming language is all about objects and manipulating data within those objects by calling methods. Listing out your application's data requirements helps you decide what kinds of objects you're going to need to create or initialize. The list above should give you a good idea of the object types you need.

Data input for the list of above consists of strings, so use text fields to collect this information. The Java API has a predefined class to create text fields to collect user input.

You could also use a text field to request information about training, but that's not appropriate for a Dive Log, since training is not necessarily in a hierarchy. Instead, try check boxes. A diver can check each completed training course.

Once the user has entered data into the fields and checked the appropriate boxes, the user needs to be able to submit the information. The most common object used to signal the application to do something with the information entered is a button.

The Java API has a handy class for creating a button component. You'll learn more about JTextField, JCheckBox, and JButton soon.

For each item, you need an object to label the object that the user types in, and you need an object to collect the listed data. In other words, for each item above, you'll need at least two objects. The next section shows what is needed and how to handle the Diver Data tabbed pane.

Planning the Diver Data Pane

To plan the Diver Data tabbed pane, itemize the objects you need and organize that data at the same time with a sketch. Use either paper and pencil, or a drawing program.

For now, list out the label, text field, and check box variable names you'll need, and organize them into a desired layout. This helps you decide:

  • The objects to create
  • Their reference variable names
  • Where they should be positioned on the pane

Once this is completed, you can begin writing the code. The image below illustrates this planning concept:


Preliminary object and layout plan

Notice that:

  • The object type is on the left of the = sign and the variable name on the right, much like you'd write it in code.
  • Label objects are aquamarine.
  • Text field objects are black.
  • Check box objects are maroon.

This makes it easy to see what objects you need to create, therefore which classes you need to initialize, and how you want those object positioned on the pane.

So far there are a total of 22 objects on this pane. You may recall that the BorderLayout class allows only one object to be added to each region, totaling five objects in all.

Can you add 22 objects to this Diver Data pane, using the border layout manager?

   A. Yes
   B. No