Lab 7: An Introduction to Swing

Copyright RIT 2009
$Id: writeup.xml,v 1.7 2009/10/15 15:18:11 vcss232 Exp $


Goal

  1. Learn how to use some basic Swing components
  2. See how to use layout managers to arrange components within containers
  3. Learn how to prototype simple GUI applications


Team Setup

You are to work on this lab completely on your own.

Overview

The purpose of this lab is to give you some experience building simple graphical user interfaces (GUIs) using Swing. This lab will focus on constructing GUIs and using layout managers to arrange the components contained within the GUI.

Pre - Lab Work

  1. Review your class notes on Java Swing.

  2. Read through the Java tutorial trail on Laying Out Components Within a Container (http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html) .

In-Lab Activities

Activity #1 : Construct a data input screen

In this activity you will use Java to write a program that creates and displays the screen shown in the picture below. Please note, your title bar will look different on the Solaris machine (these screen shots for activity 1 were taken on a Mac).


Address window

And this is how it looks if the window is resized.


Address window resized

The GUI that your program will display does not have to be programmed to respond to any user input (i.e. the buttons and text fields will be inactive). The purpose of a program such as this is to provide the programmer, or the user, with an opportunity to see what the GUI that a software system might produce will look like. In this case, this program will provide you with an opportunity to learn how to use the containers, components, and layout managers provided by the Java Swing classes.

If you are not sure which Swing components to use in this program, run the Java Swing demo by typing the following command:

$ swingset

The Swing demo will create a GUI that contains all of the components that can be created using Swing.

Rather than specify a fixed width and height for your window, use the JFrame.pack() method to automatically set the window size based on the default sizes of the components in the window.

Note: It is considered "good style" for a window class to create its content within its constructor, rather than writing a main() that creates a window instance and populates it 'from the outside' (as it were). Write your program so that the constructor's code creates the window frame, adds components, and sets properties by invoking appropriate methods. The main() method simply calls the constructor and makes the window instance visible.

You must name your source file: AddressScreen.java. The program takes no command line arguments, creates a JFrame places the appropriate components inside the frame, and becomes visible.

You must write the code required to terminate your program correctly when the user presses the window's close box. You need to call a method on your JFrame instance. Assuming your class is a subclass of JFrame, the statement to put at the end of the constructor is this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );. That will make your program terminate when the user presses the close box of the window.

How To Submit

Once you are convinced that your program is working correctly, and that the GUI is formatted correctly, submit your work using the following command:

try grd-232 lab7-1 AddressScreen.java

Activity #2 : Build a GUI to play the lottery

In this activity write a program named LottoGUI.java that will create a GUI similar to the one displayed below:


Lotto Window

Be sure to pay close attention to the colors used in the GUI. Your program must create a GUI that uses the same colors and the same layout used in the image above (the numbers are simply the default colors, but the buttons on the left have black background and green, red and white text from top to bottom. Also, all the numbers along with Draw, Reset and Quit need to be implemented as buttons. Also note that the text at the bottom should be implemented with two JLabels.

As in the previous activity your GUI does not have to be operational (i.e. buttons contained in the GUI do not need to work). You simply have to write a program that creates a GUI similar to the one shown above.

You must set up the code so that it terminates the program correctly when the user closes the window. The method to call is setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ).

Make sure you spend ample time on this activity, because you will be adding functionality to it in the next lab.

How To Submit

Once you are convinced that your program is working correctly, submit it using the following command:

try grd-232 lab7-2 LottoGUI.java

Activity #3 : Use some basic Java dialogs

The frames that you created in the previous programs are not the only type of displays used in programs that utilize a graphical user interface. Many times, as a program runs, information will be collected from the user using a dialog. Dialogs are typically modal, which means that no other work can be done in the program until the user dismisses the dialog.

Swing provides the programmer with the ability to create several different types of dialogs. For example, an instance of the JFileChooser class provides the user with the ability to select a file that can be used in a program. Other dialogs can be used to provide the user with diagnostic information and confirm user selections. The static methods contained in the JOptionPane class can be used to create a wide variety of dialogs that can be used in a program. Take a few minutes to look over the documentation for the JFileChooser and JOptionPane classes before proceeding to the next step.

Write a program named, DialogViewer, that takes a single command argument. The command line argument will be used by the program to determine what type of dialog to display. The three valid command line arguments are specified in the table below:

Command Line Argument Action

f

Display an instance of a JFileChooser. If the user selects a file, the program will print the name of the file selected by the user. If the user did not select a file an appropriate message should be printed by the program.

c

Display a confirm dialog that contains the message "Are you sure?" and after the dialog has been dismissed, display the type of button (i.e. Yes, No, or Cancel) that the user pressed to dismiss the dialog.

m

Display a message dialog box. The message displayed in the box should be your name.

You will notice that both the JFileChooser and JOptionPane classes provide a number of different methods that you can use to create the dialogs needed in this program. In this activity, always use the method that requires the fewest number of parameters. For example, when you write the code to create the JFileChooser use the default constructor.

You should notice that the static methods in the JOptionPane class, that you will use to create the confirm dialog and the message dialog, require a JFrame as a parameter. This frame is the 'parent' window of the dialog, which is said to 'pop up' from the parent. For this application, the parent always remains invisible, and the dialog simply appears. Nevertheless, there is still a parent window.

If the program is not invoked correctly, an appropriate usage message should be displayed on standard error. Since this program utilizes the AWT, a thread will be created when you display the dialogs. Since a Java program does not terminate until all of the threads within the program have terminated, you must call dispose() on your JFrame object to get stop the thread and allow your program to terminate. This means you should create a JFrame that is the 'parent' of your dialogs. For this activity the parent window never is visible. ( JFrame is a descendant of Window. ) The 'parent' window must be dispose()d at the end of the program.

How To Submit

Once you are convinced that the program is working correctly, submit it using the following command:

try grd-232 lab7-3 DialogViewer.java


Grade Computation

Grade Breakdown: