Skills to be Applied:

Swing/AWT, ArrayList (very similar to ArrayList class)

Classes may be needed: JApplet, JButton, JTextField, JTextArea, JLabel, Container, JPanel, JTabbedPane, JCheckBox, , and ActionListener. You may use other classes.

How to run an applet program:

Create an html file, say "hw6.html" with the following content:


--------------------------------------------------------
< html>
< head>
< title>Assignment 6 Applet< /title>
< /head>
< body>

< applet code="Assignment6.class" width=500 height=400>
< /applet>

< /body>
< /html>
------------------------------------------------------

-

Compile your java program as usual.

In a console, type:

appletviewer hw6.html

(instead of typing "java Assignment6")

In the TextPad, choose Tool->Run Java Applet

In the jGrasp,choose Run->Run as Applet.

Program Description

Suggested Class Diagram: see image.

Write a Java program that constructs an Applet. Your program should provide labels and textfields to a user to enter information regarding flights.

The Applet (JApplet) of your program should contain two tabs. The first tab is labeled "Flight Creation" and the second tab is labeled "Flight Selection".

(The size of the applet here is approximately 800 X 200).

The section under the first tab should be divided into two parts:

The left part contains labels, textfields, and a button for a user to enter some flight information. The right part shows "No flight" at the beginning (it is done using JTextArea). see image.

A user can enter some flight information, and push "Create a flight" button. see image.

Then the flight information should appear on the right hand side panel (note that the format of the flight information can be using toString() method of the Flight class). A message "Flight added" should also appear with red color at the top of the panel. see image.

Error handling:

If a user forgets to enter some field and pushes "Create a flight" button, show a message "Please enter all fields." with red color, and nothing should be added to the right hand side panel. see image.

If a user enters non-numeric value for its flight number or airfare and pushes "Create a flight" button, show a message "Please enter a number for its flight number and its airfare" with red color, and nothing should be added to the right hand side panel. see image.

After entering several flights, the applet will have the following appearance. Note that a scroll pane needs to be added to show multiple flights. see image.

Under the "Flight Selection" tab, a user can choose flights from There should be a JList containing all flights created by Flight Creation panel and a user can choose flights by clicking its check box.

The list of flights in the Selection Panel should be exactly same as the list under "Flight Creation" tab. see image.

After choosing some flights by checking some check boxes, it should show the total airfare of the chosen flights in the JTextField below. This total purchase amount should be updated everytime a user check or uncheck each check box. see image.

A user should be able to go back and forth between "Flight Creation" tab and "Flight Selection" tab, and these two panels need to have consistent information, i.e., the same list of flights.

Class description

SelectPanel

SelectPanel class extends JPanel defined in javax.swing package. It should contain at least the following instance variable:

Attribute name, Attribute type, Description
flightList, ArrayList, a list of Flight objects.

This class should have a constructor:

public SelectPanel(ArrayList flightList)

where the parameter "flightList" is passed from the Assignment6 class. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including a JLabel, a JTextField,and JCheckBox.

public void addCheckBox(Flight flight1)

This method create an object of JCheckBox using the toString of the Flight object parameter. This JCheckBox object needs to be added to the panel to be displayed. An object of CheckBoxListener class needs to be added to this JCheckBox so that it listens when a user checks or un-checks. This method needs to be called from the actionPerformed of ButtonListener of the CreatePanel class when a new Flight object is added.

This class contains a nested class called CheckBoxListener class that implements ItemListener interface. Thus you need to define its itemStateChanged method that is supposed to verify which check boxes are checked and add the airfare of the corresponding flights and display the total purchase amount to the JLabel using the dollar format (Please use NumberFormat class).

CreatePanel

CreatePanel extends JPanel defined in the javax.swing package. It should contain at least the following instance variable:

Attribute name, Attribute type, Description
flightList, ArrayList, a list of Flight objects.
selectPanel, SelectPanel, an object of SelectPanel.

This class should have a constructor:

public CreatePanel(ArrayList flightList, SelectPanel selectPanel)

where the parameter "flightList" is passed from the Assignment6 class and the second parameter is an object of SelectPanel. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including labels, textfields, a button, and a text area.

This class contains a nested class called ButtonListener class that implements ActionListener interface. Thus the ButtonListener needs to have a definition for actionPerformed method that adds some flight information to the list and does error handling. See the UML class diagram for the parameter and return type of this method. In the actionPerformed, you need to extract the information from the textfields for flight information. Then you can instantiate an object of the Flight class using the information. You can use the toString( ) method of the Flight object to display the information on the textarea on the right hand side and also add the Flight object to the "flightList". This is also where addCheckBox( ) method of the SelectPanel class needs to be called with its object "selectPanel". so that a new flight is added to the CreatePanel, the same one is added to the SelectPanel.

Assignment6 class

Assignment6 class extends JApplet defined in javax.swing package. It contains at least init() method (see UML diagram for its return type) to instantiate all instance variables and adds its components to itself. It also sets its size. It contains at least following instance variables:

Attribute name, Attribute type, Description
flightList, ArrayList, a list of Flight objects. It will be used in both CreatePanel and SelectPanel.
selectPanel, SelectPanel, an object of SelectPanel.
createPanel, CreatePanel, an object of CreatePanel.
tPane, JTabbedPane, an object of JTabbedPane. It will contain createPanel and selectPanel under each tab.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.