CS401 Lab 8: Using Simple Graphical Components

Note: This lab is graded and will be worth 2 points toward your overall grade.  You will have two lab sessions to complete it.  Use the first session to familiarize yourself with graphical components and use the second to complete the actual exercise.  You must complete your demonstration by the end of your second lab session in order to receive the 2 points.


Introduction and Background

We have just started discussing Java graphical applications and event-driven programming.  In these applications the user interacts with the program via the use of a graphical interface -- for example a window with buttons and textfields and dialog boxes.  Most consumer-oriented computer applications have graphical interfaces, so it is a good idea to get some practice in designing and implementing these applications.

In Lab 7 you completed the implementation of a simple Movie DB program, which allowed the user to list movies, search for a movie and add a new movie to the DB.  The movies were loaded from a file and saved back to the file when the program terminated.  You will now convert this program into one with a graphical interface.  However the Movie class and MovieDB class will be IDENTICAL to those from Lab 7 (see files Movie.java and MovieDB.java). We are not changing the functionality of the DB, just the interface with the user.

To make the lab more feasible and more consistent, I have already written all but the event handlers for the main functionality of the program.  Download this code and read it thoroughly (Lab8.java).  For this lab you must implement the ControlListener class (which is the ActionListener for the JButtons) so that the program functions in a good, reasonable way.


Program

The Lab8.java program that you download should compile and run as given to you (as long as you also downloaded the Movie.java and MovieDB.java files).  However, as given the buttons do nothing when clicked, because the actionPerformed() method of the ControlListener class contains no actual code.  Run the program and look at the interface, then implement the ControlListener class as specified below:  The functional aspects of the program should be as follows:

  1. The List Movies button.  This button should call the toString() method of the MoviesDB object (movies) and append the result to the JTextArea.
  2. The Add Movie button.  This button should prompt the user (via JOptionPane.showInputDialog() boxes) for all of the components of the new movie, then create the Movie object and add it to the MovieDB (movies) variable.  Finally, the new movie should be printed to the JTextArea to show the user that it has been added.
  3. The Find Movie button.  This button should prompt the user (via a JOptionPane.showInputDialg() box) for the name of the movie to be found.  It should then call the appropriate method in the MovieDB (movies) variable to search for the movie.  If the movie name is found, it should print out the movie info to the JTextArea; otherwise it should print out that it is not found.
  4. The Quit Program button.  The user should be asked to confirm the quit using a JOptionPane.showConfirmDialog() box.  If the confirmation is given, the MovieDB should be saved back to the file and the program will be terminated.   To terminate a Java graphical application, you can issue the statement System.exit(0);.

For options 1-3 above you must update the JTextArea in your program.  This should be done in a nice way, so that only the data that is needed is shown and so that the JFrame is sized just enough to hold the data in the JTextArea (hint: pack()).  See the demonstration by the TA for how your program should work.


Grading

Demonstrate that your program works correctly to your TA by running it for him / her.

Answer the question asked by your TA.

Your demonstration is due by your lab session the week of November 7-10 (see Lab page for your specific dates)

This lab is worth 2 grade points, distributed as shown below.

  1. Show your TA that Lab8.java compiles and executes correctly with your code (15 points)
  2. Answer a short question about how you solved the problem (5 points)

Since this lab is worth 2 points for your final grade, the points received will be divided by 10.0 to determine your final score.


Notes and Hints

Read the Lab8.java code given to you VERY carefully before starting your own code.  Also read over the various handouts that were given on graphics and graphical components.  You should also look up some of the needed classes in the Java API (ex: JTextArea, JOptionPane) so you know what methods to call and what arguments they require.  Think logically about what is supposed to happen in each situation and write the handler appropriately.