CSC 160
Lab Exercise 6

6 November 2008
5 Points
due at end of lab session

Developed by Susan Haller and Timothy Fossum, University of Wisconsin-Parkside, to accompany Chapter 14 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu. Adapted by Pete Sanderson


Event-Driven Programming and GUIs

There are 3 written questions and 5 checkpoints . If you need help with any exercise, raise your hand.

Getting Started

I have placed the lab materials, folder Lab6, into your individual folder in the course repository (not in the public folder). You probably called this folder myFiles. Update it and you should be ready to go.

Launch jGRASP.

Extending the JFrame Class

Go into subdirectory FirstFrame and edit the class MyJframe. This class extends JFrame.

Write your answers to these questions on your answer sheet:

  1. Why is there no dot notation used on the methods: setTitle(), setSize(), setDefaultCloseOperation(), and getContentPane()?
  2. Why is the dot notation used with the method SetBackground()?

Predict what will happen when this class is compiled and run. Compile and run to see if your prediction is correct.

Adding JButtons to the ContentPane

Go into subdirectory SecondFrame and edit the MyJFrame class in this directory. Examine the code. Notice the layout manager is explicitly set to null, meaning that absolute positioning of GUI components will be performed using the setBounds() method inherited from java.awt.Component.

Predict what will happen when you run this program. Compile and run it. Without changing the size of the window or the button, position the button so that it is completely within the window.

Write your answer to this question on your answer sheet:

  1. What change did you make to the program?

Handling A Button Event

It would be more interesting if something happened when you click on the button

Go into subdirectory ThirdFrame and edit the MyJFrame class here. Examine the code. Notice that MyJFrame implements the ActionListener interface. Next look at the MyJFrame constructor. When a MyJFrame object is constructed, it is added as a listener to the button displayed in its content pane. Notice that the button is labeled "0".

Because this version of MyJFrame implements the ActionListener interface, it must have an actionPerformed() method that is passed an ActionEvent to handle. Right now, the actionPerformed() method does nothing with the button event. Compile and run this program and notice that nothing happens when you click on the button.

Add code to the actionPerformed() method so that each time you click on the button, the number displayed increments by one. For example, when you click on it the first time a "1" should appear, the second time you click on it a "2" should appear and so on.

Hint: Add a new private integer instance variable buttonCount that keeps track of how many times the button has been clicked on. You should initialize this variable in the MyJFrame constructor. Every time the button is clicked, increment this variable and display the new value on the button. In your Java documentation, look up the setText() method that the JButton class inherits from its parent class AbstractButton(). Also, look at how the JButton was constructed for some ideas about how to convert the value of buttonCount to a String. You can pass this string as an argument to the button's setText() method to set the label on the button to the new value of your counter variable.

1 Call me over to see your code and running program.

Handling More than One Button's Events

Go into subdirectory FourthFrame and edit the MyJFrame class in this subdirectory. In this version, MyJFrames are constructed with two buttons both labeled "0". Notice that a MyJFrame object listens for action events from both buttons. This means that when an action event occurs, the event handler actionPerformed() must get the event source from its ActionEvent parameter and check to see which button (button1 or button2) was clicked. Once you know which button, the event handler should increment that button's count (button1Count or button2Count) and redisplay the new count on the apprpriate button. Modify the actionPerformed() method accordingly.

2 Call me over to see your code and running program.

Handling Text Events

Go into subdirectory FifthFrame and edit the MyJFrame class in this subdirectory. Compile and run this program. The button works just like it did in ThirdFrame. Notice that whenever you type something into the text field and hit ENTER, the button increments its number. This is because the actionListener() method doesn't distinguish between an event from the button or the text box.

Modify your actionListener() code so that when you enter text in the text field and hit ENTER, the handler extracts the String from the text and turns it into an int (use Integer.parseInt()). This value should then be used to update the buttonCount value and also the label on the button. For example, suppose the button currently displays "2" and you enter "77" into the text field and hit ENTER. The button should display "77". Furthermore, if you follow this with clicking on the button, it should display "78" and so on. As with the previous checkpoint, your actionListener()code can determine the source of the event (either the text box or the button) and act accordingly.

3 Call me over to see your modification and running program.

For the next checkpoint, add a label to this version of MyJFrame, positioned above the text field, that says "Enter a number to update the button". This is to let the user know what should be entered into the text field. The label should be centered (as best you can) just above the text field. You may need to experiment with a few position values to get the label to look right. If you want to change the label text color, use its (inherited) setForeground() method.

4 Call me over to see your modification and running program.

Handling Menu Selection

For the last checkpoint, continue working with the same program. Add to MyJFrame a menu called File that has two menu items, one called Clear and the other called Exit. Follow the example in section 14.7 of your text. You will need additional instance variables for the menu and the two menu items. Use
JMenu menu;
JMenuItem clearItem;
JMenuItem exitItem;
Don't forget to create a JMenuBar object and add it to the frame using setJMenuBar(). As before, use the MyJFrame object as the action listener for each of the two menu items, but do not change your actionListener() code yet.

Compile your program and check to see that the menu appears and that you can "select" either of the two menu items labeled Clear and Exit, but that nothing happens when you do.

Finally, change the actionListener() code so that if the event source is clearItem, the value of buttonCount will be reset to zero (and the button text will change accordingly) and if the event source is exitItem, the program will terminate immediately using System.exit(0).

Make these changes, and compile and test your program.

5 Show me your modifications and demonstrate that your program works correctly making appropriate menu selections.