CSC 121A Lab 4 - Spring 2005

Thursday, January 27, 2005
Due: Wednesday, February 2, 2005, 5:00PM

[Course home page] [Java API Documentation]

Answer all numbered and bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, please turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)

 

1. Dialog Boxes and Parent Frames

Download the following files and run the TextFieldTest program that we went over in class: [TextFieldFrame.java, TextFieldTest.java]. As we saw in class, when you press in one of the fields, a dialog box pops up on the screen.

  1. Where on the screen does the dialog box pop up?
     
  2. On line 85 of the TextFieldFrame.java file, change the null argument to the showMessageDialog method call to: TextFieldFrame.this. What effect does this have on where the dialog box pops up? Why? (You may resize the frame to a large size in order to see the difference in behavior.)
     

We mentioned in class that the TextFieldHandler class is an inner class because it is defined inside of the TextFieldFrame class. Thus, as an inner class, the TextFieldHandler class has access to the TextFieldFrame object with which it is associated. The expression TextFieldFrame.this refers to the TextFieldFrame object. (Notice, also that the inner class can directly refer to the fields of the outer level object -- textField1, textField2, etc.)

 

2. The JButton class

Download the following files and run the ButtonTest program: [ButtonFrame.java, ButtonTest.java, bug1.gif, bug2.gif].

  1. What happens when you move the mouse over the "Fancy Button"? What part of the code do you think causes this to happen?
     
  2. What happens when you click on one of the buttons?
     

 

3. The JCheckBox class

Download the following files and run the CheckBoxTest program: [CheckBoxFrame.java, CheckBoxTest.java]. Check boxes generate different types of events than buttons or textfields.

  1. What type of event does the handler for a check box handle?
     
  2. What happens if you check both of the checkboxes?
     
  3. In the event handler for the check box, the font of the textfield is changed. How is this acheived? What is the second argument that is passed to the constructor?
     

 

4. JList

Download the following files and run the ListTest program: [ListFrame.java, ListTest.java]. You should see a list of colors in a frame.

  1. What happens when you click on the name of a color?
     
  2. What do you have to do to have the list also display and process the colors orange, pink, red, white, and yellow?
     

This code uses an example of an anonymous inner class. We will discuss more about this in class, or you may ask me about it.

 

5. Mouse Event Handling

Mouse events are handled through MouseListener and MouseMotionListener interfaces. Mouse events can be trapped and handled for any AWT or Swing component if an appropriate event-listener object is registered with the component.

  1. What are the methods of the MouseListener and MouseMotionListener interfaces? (That is, what sort of events can be caught and handled by components?)
     

Each of the mouse event-handling methods takes a MouseEvent object as its argument. A MouseEvent object contains information about the mouse event that occurred, including the x- and y-coordinates of the location where the event (such as a mouse click) occurred. Also, the methods and fields of the InputEvent class (the superclass of MouseEvent) allow you to determine which mouse button was pressed (if applicable).

Download and run the following MouseTracker program: [MouseTrackerFrame.java, MouseTracker.java]. Examine the code and ask me if there is anything which you do not understand in it.

 

6. Programming Exercises (complete for homework)

 

Based on material from Deitel & Deitel, Java How to Program, 6e.



Last modified: Thu Jan 27 09:26:32 EST 2005