JamesMadisonUniversity

CS Dept


CS 159 Lab: Handling Exceptions


Objectives:

Students will:
  • begin working with GUI components
  • apply the concept of interfaces to GUI applications

Background:

GUI or graphical user interfaces are the way most of us communicate with computer programs. Tools such as Eclipse and JGrasp have a graphical interface with which we interact. But GUI applications are code, just like all applications, and using the rich component libraries of the Java swing class and awt, you can begin to build your own graphical front ends.

Listeners are application software that "listen" for the actions of some component of the system, like the user pressing a key. These events can trigger specific code to run when the specified action is carried out. We will be working with an ActionListener in this application. ActionListener is in interface in Java and specifies that the actionPerformed event be implemented. Once a class implements the ActionListener, it can be added to GUI components to carry out the intended action.

New Terms:

ActionListener

GUI

Components

Materials:

Clicker.java, ClickerDriver.java ExpressionDriver.java ExpressionsGUI.java, ExpressionParser.class

Acknowledgment:

Clicker is a variant of a Tutorial in the Java Tutorials library (http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html)

1 Setting up the environment for this lab:

  1. Create an Eclipse project for this lab.
  2. Drag the Clicker.java starter file to this project. Drag ClickerStart.java also to the folder. It contains the main for this applicatioin.
  3. Drag the ExpressionsGUI.java starter file to this project. Drag Expressions.java to the project. It contains the main method for this application.
  4. Either use your own ExpressionParser.java and drag it into the project or create a new class folder in the project and save the ExpressionParser class to it. (Project/Preferences/JavaBuildPath/Libraries/Add Class Folder and create a folder for the class. Copy the class file into this folder).

2 Exploring GUI

First an orientation to some of the GUI terms. A Frame object is a window with a border and a body. In this lab, we will use a specialization of Frame which we will call Clicker. A Clicker objectt contains in its body a Button object and a TextField object.

Frame diagram

So now let's look at how such a Frame is built in Java code.

  1. Open the Clicker.java program.
  2. Read the program looking through the documentation. Some things to note:
  3. Run the ClickerDriver program which instantiates the Clicker window and makes it visible to the user. You should see a small window appear with the two components displayed as shown above.
  4. Click the button. What happens?
  5. So while this is a window with an ActionListener attached to the button, there is no code to say what to do when the button is pressed.

3 What does it mean to be an ActionListener

  1. Comment out the code that that implements the ActionLIstener in the class header. It should look something like this: public class Clicker extends Frame implements WindowListener //, ActionListener
  2. Does Eclipse mark any errors?
  3. Check out the Button component in the Java APIs: http://docs.oracle.com/javase/7/docs/api/java/awt/Button.html
  4. Look at the addActionListener method. It takes in an ActionLIstener object. When you commented out the ActionListener in the implements clause, was Clicker an ActionListener any longer?
  5. Go back and uncomment the ActionListener code that you had commented in step 1, so that Clicker is the ActionLIstener that the Button object needs..

4 Now, let's write an actionPerformed method.

  1. We want the actionPerformed method to do two things. It should:
    1. Increment the numClicks variable and
    2. Change the text in the TextField object to "Button Clicked " + numClicks + " times" (See TextField and setText method. http://docs.oracle.com/javase/7/docs/api/java/awt/TextField.html) or (hint1)
  2. Implement those changes and test your work.

5 Just for Fun

  1. Add some code to the actionPerformed method to change the color of the Button object based on whether the number of clicks is odd or even. (What method can change the background color of a component?) Go back to the Button documentation or (hint2)
  2. Test it.

6 One more application

  1. You've just finished (or are about to finish) PA4, the expression parser. Let's build a front end to that that uses a GUI window.
  2. Open the ExpressionsGUI.java provided with this lab. It will create a Frame with three components. Your task is to fill in the actionPerformed method to fully implement the ActionListener.
  3. First, note the name of the two TextFields. These provide the places for input and output in the Frame. The output field has been protected so only the program can put new data into it. The Button name has been changed to "submit".
  4. In the actionPerformed method, you should get the Text from the TextField (see API) or (hint3) and pass the resulting String to the ExpressionParser.isExpr() method. You should store the result of the isExpr call.
  5. If the call is true, you should set the Text in the TextField to the String "Correct". See the Clicker actionPerformed method for an example of how to do that. If it returns false, you should set the text to "Incorrect" and change the background color of the result box to RED. (A text box uses the same method for changing the background color as a button.
  6. Test your work.

Turning in your work

  1. For Prof Nancy Harris, Sections 1, 4, 5: Either demonstrate your working program in lab, or upload the completed ExpressionsGUI.java and Clicker.java programs to Canvas.
  2. For Prof Arch Harris, Sections 2, 3, demonstrate your program as far as you have gotten before you leave the lab.

 


Updated 03/27/2014 (nlh)