In Part 4, you learned to handle conditions and problems so you can read from and write to files. You also learned the
Box layout, how to create pop-up windows for warning messages, and how to use the
JFileChooser class for naming new files.
Part 5 reinforces these concepts and introduces the second part of the Log Dives pane, showing you how to:
Vectorobjects to store input.
For this part of the tutorial, you will change two classes you previously created:
CenterPanel.java . In addition, you'll define new classes, renaming the
WestPanel.java placeholder class to
DiveHandler, and create two other classes called
Dives.java: Initializes the two classes (
DiveHandler) that make up the Log Dives pane.
UIWestPanel.java: Initializes the GUI features, such as text fields, radio buttons, a drop-down menu, and buttons for entering and viewing dives.
CenterPanel.java: Is no longer needed. The
JTextAreaand the shark image are moved into the
DiveRecord.java: Models what a
DiveHandler.java: Provides functionality, such as collecting the data from the
UIWestPanelobject, and saving the
DiveRecordobject to a file.
This lesson shows you how to build a user interface that allows a user to enter information that is saved for each dive, and a separate class that collects data from the user interface into a
DiveRecord object through
public get methods. Lastly, you create code that writes the object to a file, which can later be read back into the GUI.
The Log Dives pane is built in four basic steps:
DiveRecordclass for the
Note: It's assumed you installed the Java 2 Platform, Standard Edition (J2SE) on your system, and that you have completed Part 1, Part 2, Part 3, and Part 4 of the Building an Application tutorial.
In Part 4 of the Building an Application series, you learned about
JFileChooser, and how to text to a file. The
CenterPanel creates the text area for the user to enter text. You keep the code that creates the text area and shark image, but you no longer need the rest of the code. Because the text area is a GUI feature, the code for it needs to be included with the other GUI features of this pane.
Note: You cannot compile this class until you create the
Until now you've built each pane from a single user-defined class. These classes included code for both GUI components, the data, and the functionality of the pane. A better way of designing software involes the Model, View, Controller (MVC) architecture.
The idea behind MVC is to separate the graphical (or view) components from the data (or model), and use a controller to deal with user interaction and modifying the model or view as appropriate:
Of course, some applications are small or have little functionality, so the MVC architecture is overkill. But for this pane, MVC works well.
The classes of the Log Dives pane serve as an example:
UIWestPanel.java: Creates the GUI components for the user to enter information. This class acts as the view.
DiveRecord.java: Contains a contructor to build a
DiveRecordobject that eventually holds all the user input, and has the
setmethods necessary to access indiviual data. This class defines a model for a
DiveHandler.javais the controller. This class instantiates the
DiveRecordclasses. In addition, it provides the functionality of the buttons for the
UIWestPanelclass by calling the
getmethods of the other classes.
Now that you've made necessary changes to the two classes you created previously, you're ready to design a dive object class. This is the plan for what each dive consists of. It also tells you what type of GUI features you'll need to collect information from the diver, because each dive object consists of dive data only.
To decide what the
DiveRecord class constructor needs passed into it, determine the most common information divers provide for each dive, and note what type of data it is, such as:
Each piece of information above is assigned to a type such as a
String or an
int, and assigned an identifier. The dive object is constructed at the time when the diver enters all of the information.
This class then needs to include the following:
getmethods for each information item
That's all. The instructions for what should be done with that information go into the controller class that you'll construct last.
DiveRecord class provides a constructor for the data that is collected, and methods to work with that data. Because you're going to write this object to a file using serialization, this class implements
Usually objects last only as long as the program that creates them is up and running. The ability of an object to save, or record, its state so it can be reproduced in the same application or a different one is persistence. Objects persist by being recorded through writing their values that describe their state in a file. For instance, let's say you've created a Car class so users can create their own Car objects according to their needs. Persistence would allow each Car instance to save its make, model, color, and so forth for each individual Car object and record the state of each car to be brought up at a later time.
You save an object's state through object serialization, which is represented by an ordered series of bytes. If that state information is another object, the current settings for that object are also saved, and so on until everything is a primitive data type. Methods and
static variable state are not saved. Methods consist of behavior and not state, so they are not saved. Static variable state is information about the class, not the instance, so it isn't saved either.
Serialization is ideal for saving the dive data because you only need the data saved. A
DiveRecord is simply an object with that data.
If a class implements the
java.io.Serializable interface, it is serializable. There are no methods to the interface, so it just serves as a marker interface. However, simply implementing the interface isn't sufficient. The instance variables of that class must also be serializable; otherwise, when you try to serialize the state, an exception will be thrown.
Later you'll learn about the methods that write and read the object to and from a file. For now, just make sure that your
DiveRecord class implements the
Your new class should look like this DiveRecord.java example.
Note: The keyword
this used in the constructor above. Argument names hide the member variables. So identifers within the body of the constructor refer to the argument, not to the member variable. To access the member variable, you must reference it through
this, the current object. Typically, within an object's method body, you can refer directly to the object's member variables. However, sometimes you need to distinguish the member identifier if one of the arguments to the method has the same name. This is done simply with the keyword
That completes the model for the dive log object. Now that you have the model object class completed, you need to create the GUI components the user needs to enter data.
The Logs Dives pane contains a text area for comments about dives and other useful graphical input components, such as a drop-down menu to choose a dive depth, two radio buttons to toggle between shore or boat dives, and the familiar text fields to record date, bottom time, visibility, and a label that gets filled in by a method you create that automatically records the dive number. If you're building, for example, an exercise log instead, you can change these features to record calories burned, aeorbic and non-aerobic exercise, and so forth. Change these features as you see fit for your application.
Next, you'll create a
UIWestPanel class to define the GUI components for a diver to enter dive data.
|Read the API for class JComboBox. Which line of code is correct to make the combo box editable?|
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.