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

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

Yes, you can add 22 objects (or more) to the Diver Data pane using the border layout by adding panels with their own layout managers to the regions of the border layout. Each panel then has additional objects, making it possible to create numerous objects on this pane. This lesson describes the details.

More on Variables and Access Control Declaring Member Variables

Basics of Objects

Setting Up the Diver Data Pane

The Diver Data pane contains labels to signal what information users should enter in the text fields and check boxes. In addition, an image is included for aesthetic purposes. These objects are arranged so that personal diver data appears at the top of the pane, training information to the far right, the image to the center and left, and emergency information at the bottom. In addition to listing variable and object information, the sketch also reveals a layout pattern.


Layout of Diver Data Pane

The image to the left shows four basic areas that can easily become regions of the border layout. Since you have more than five objects to add, you need to create panels to hold these objects, and the panels are then added to the four regions. The layouts for each panel vary, depending on the requirements of the objects added.

Like the Welcome pane, the Diver Data pane starts with a plan, the Diver class. Use the placeholder Diver.java class you created in Part 1. This class should already use the keyword extends to make this pane a JPanel through inheritance.


Building the Diver class requires the following steps:

  1. Declare the necessary variables for each label, text field, check box, and image object.
  2. Declare a constructor.
    Within the constructor:
    1. Set the layout and background color.
    2. Initialize each object previously declared.
    3. Call methods to create each panel.
    4. Add each constructed panel to the border layout regions of the Diver pane.
  3. Define the methods that construct each panel.
  4. Create a class to handle the check boxes.

Since you've already listed the variables in a sketch, you can use this listing to declare the variables in the Diver class.


  1. Open the Diver.java file in your text editor.
  2. Add the following list of variables just after the opening curly brace of the class declaration:
  3.      // Text fields for user input
         private JTextField name; 
         private JTextField street; 
         private JTextField city; 
         private JTextField statezip;  
                 
         // Labels to go with each text field
         private JLabel lname;
         private JLabel lstreet;
         private JLabel lcity;
         private JLabel lstatezip;          
                
         // Check boxes for types of diver training
         private JCheckBox ow;
         private JCheckBox a;
         private JCheckBox res;
         private JCheckBox un;
         private JCheckBox w;
                 
         // Text fields for Emergency box
         private JTextField nname;
         private JTextField phone;
         private JTextField rel;
         
         // Text fields for Emergency Contact
         private JLabel lnname;
         private JLabel lphone;
         private JLabel lrel;
         
         // Buttons and image
         private JButton enter; 
         private JButton edit; 
         private JLabel seahorse; 
                 
         // Panels to be built and added
         // to the border layout of this
         // panel.
                 
         private JPanel images;
         private JPanel jaddress;
         private JPanel emerg;
         private JPanel training;
         // Class to handle functionality of check boxes  
         ItemListener handler = new CheckBoxHandler();
         // ItemListener and CheckBoxHandler classes are 
         // explained later.
    
  4. Save the file.

Note that all the variables are declared private. This protects the data from being manipulated from outside classes. Declaring data private and some methods public is a part of a concept called encapsulation.

Encapsulation and Access Control

In Part 1 you learned a little about the access attributes: private, public, and protected. Access control plays a big part in encapsulation. Encapsulation refers to how to hide and protect data and methods within an object from outside interference and misuse. The class definition is the foundation for encapsulation.

Classes define the instructions for the type of access protection each data item and method has, and which methods have access to that data. In addition, classes working together take advantage of hiding the workings of methods and objects. In other words, encapsulation aides you in building applications with predefined objects and objects of your own making.

You don't need to understand the mechanics of the predefined classes you use in the same way you don't need to be concerned with the workings of a radio you install in your car. A class from the Java API is a predefined component just as a radio is a predefined component.

In designing and writing the code for the Dive Log, you have already dealt with encapsulation in two ways:

  1. Hidden Implementation:
    • Initializing objects from predefined classes
      Consider the JFrame object. By initializing or using the extends keyword to create a JFrame object, you created a frame with minimize and maximize buttons, and an area that can contain other objects. You likely have no knowledge of how this object is built. The details of building the frame are hidden from you. All you did was create a frame using either the new or extends keyword with JFrame, calling the constructor necessary to build a frame the way you want it.
    • Calling predefined methods
      When you called setBackground and passed in the Color class, you didn't need to know how that method paints the background to the color field you chose. The setBackground method isn't defined in a class you created. Instead, setBackground comes from another predefined class. The instructions of this method are hidden from you. You only call the method on the object you want to add a color background to, and the method did the work for you.
  2. Access Control
    • The predefined methods you've called so far have been declared public, giving you access to them.
    • The methods you defined in the Dive Log classes were also given public access.
    • The data variables you declared were given private access to protect them.

Safe programming practices encourages private access control on data variables. To access that data, you define public methods to either get the data or set the data. To prevent data from being manipulated, you declare the data private and do not provide access methods to that data.

The setBackground method is an example on an accessor method that allows you to set the background color of an object.

In creating the Diver class, you call predefined accessor methods, taking advantage of encapsulation. Later in this tutorial series, you define other types of access methods, some of which are declared private to protect data.

Defining the Constructor

With the variables declared, you can move onto defining the constructor for the Diver class. Here's how this constructor builds the Diver Data pane:

  • Sets the border layout manager
  • Sets a white background
  • Initializes the objects for the pane
  • Calls four methods to build four panels
  • Adds the panels to the pane

Recall that the class constructor must have the same name as the class, and it does not return a value. It does provide the instructions for how the object is built on initialization, including other objects that need to be initialized. In addition, this constructor needs the public access modifier so that the DiveLog class can initialize this class when it calls the Diver constructor.


  1. Open the Diver.java file in your text editor.
  2. Begin the definition of the class constructor and calling the methods to set the layout manager and set the background color to white. Place the following code immediately after the variable declarations:
  3. public Diver()
     { // Opens Constructor
       // Sets layout for Diver panel
       setLayout(new BorderLayout());
       // Sets background color
       setBackground(Color.white); 
       
       
     } // Closes Constructor
    
  4. Save the file.


The next step is to initialize each variable you previously declared at the top of the class.

How you initialize these objects depends on which constructor for that type you call. Remember, the keyword new signals the compiler that you are calling the constructor of the class you name. The documentation for each class defines constructors for each of these objects.

Construct a JTextField object that initializes with the text Enter Your Name and assign it to the variable name. Which line of code is correct?
   A.   name = JTextField("Enter Your Name");
   B.   name = new JTextField(Enter Your Name);
   C.   Neither