CSCI2014 Software Construction with JavaExercises: GUIs and Event Handling

Exercises 3: GUIs and Event Handling

Objectives

The objectives for these exercises are to help you

  • Use javax.swing components to construct a Graphical User Interface
  • handle events generated by a GUI;
  • Collect data from a simple form.

Reading

  • Chapter 12, Graphical User Interfaces of Horstmann provides background reading and reference material for this set of Exercises

On-line tutorials are another source of explanations and examples. Especially:

  • The Sun Java Tutorial site: java.sun.com/docs/books/tutorial

The Java API for the GUI components often have a “How to Use…” link, which has examples of program code.

Exercises

Graphical User Interfaces

1Produce a class JFrameDemo to show how to create and display some GUI components that are found in the javax.swing package: e.g.

  • JLabel
  • JTextField
  • JButton,
  • JComboBox
  • JRadioButton
  • JSlider
  • JtextArea
  • JSpinner
  • JList

A partial program is provided on the website to help get you started.. Use the Java API and tutorial links to see examples of how to create these GUI objects.

2Try different Layout Managers in your JFrameDemo program, e.g. FlowLayout, BoxLayout, or using a JTabbedPane.

3A student is required to complete an on-screen form to request an appointment with a lecturer. Design and implement a simple Form using: JLabels as prompts and JTextFields as input fields to collect a student’s name and email address; a JComboBox to select an appointment time; and a group of radio buttons to indicate whether the meeting is mainly about Personal, Academic, Financial, or Other reasons. A button panel should have "Submit", "Clear" and "Close" buttons. This exercise just focuses on the layout. There is no need to implement any of the event handlers (yet).

4(Extension work, for those who have time.) Add a menu bar and menu items to your JFrameDemo. There are plenty of examples in books and on-line to help you do this.

Event Handling

5Refer to Qu 1, the JFrameDemo program. Associate a listener with each component (apart from JLabel), and implement the event handling method. Keep the action simple e.g. print a message to the system output showing that you are in the event handling method. See example on the website to get you started.

6Refer to Qu 3, the appointment form. Attach event handlers to the Buttons on the form.

  • "Submit" should use the data from the input fields to construct an Appointment object. (You may wish to extend the Appointment class to capture the additional information.)

After the Appointment has been created, it should be

  • output using System.out.println
  • appended to an ArrayList.

Finally a confirmation message should be displayed to tell the student that the Appointment has been registered.

  • "Clear" blanks all the input fields.
  • "Close" prints out all the Appointments collected in the ArrayList, and then terminates the program.

lz/csci2014/GUIExercises.doc/oct03Page 1 of 2