Part 2:
ActionListener, instanceof, interacting objects

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should be able to:
  • Define a subclass of the JFrame class, using inheritance.
  • Write graphical user interface (GUI) application programs, using JButton, JLabel, and JTextArea objects from the javax.swing package.
  • Write GUI application programs with menus, using menu objects from the javax.swing package.
  • Write event-driven programs using Java's delegation-based event model.

Items in red are learning objectives for this part of the project.


Group work

BasicGUIObjects report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your BasicGUIObjectsReport.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in ? to ? minutes after seeing their instructor demonstrate the ideas.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.


  1. Open your BasicGUIObjects project and your BasicGUIObjectsReport.txt file. Add a heading to your report to indicate the beginning of Part 2.
  2. Skim this document. Then return to this point.
  3. Question: About how many minutes do you think it will take you to complete Part 2 of BasicGUIObjects?
  4. Question: What time is it now?

    Documenting the Project

  5. Write JavaDoc comments based on the planned behavior of the project. Include all of the required tags. Ensure that you can generate the HTML documentation correctly.
  6. The ActionListener Interface

  7. In order to respond to button clicks, we must register an object that will do the work. We'll begin by making the main frame of the application respond to the exit button. Add the following statement to the constructor of the BasicGUIObjects class, anywhere after the exit button has been created:
          exitButton.addActionListener( this );
  8. Question: What compiler error do you get when you try to apply the addActionListener to an object that is not an ActionListener?
  9. The parameter of the addActionListener method must be an ActionListener, but this refers to a BasicGUIObjects object. Add the clause implements ActionListener to the header of the BasicGUIObjects class definition. You'll need the following import statement, even though you should already be importing java.awt.*:
          import java.awt.event.*
  10. Question: What compiler error do you get when you use the implements clause without providing all of the methods that are required by the interface?
  11. Classes that implement interfaces must provide the methods specified in the interface. For the ActionListener interface, the actionPerformed method must be present, and it will be invoked whenever an event occurs for which the object is registered as a listener. In our case, it will be invoked whenever the exit button is clicked. Add the following method to the BasicGUIOjbects class:
          public void actionPerformed( ActionEvent e ) {
             System.out.println (
                "That tickles"
  12. Question: What happens when you click the exit button?
  13. Replace the print statement with the statement System.exit( 0 );
  14. Question: Express your feelings about finally making the exit button work. :-)
  15. Modify the AddPanel constructor so that each AddPanel object will be an ActionListener for the JButton object that it contains. Have it display a message on the console whenever an event occurs.
  16. The instanceof operator

  17. Register the AddPanel object as an ActionListener for its JTextField as well.
  18. Question: What happens when you type something in the text field and then press Enter?
  19. The ActionEvent parameter of the actionPerformed method can provide information about the event that caused the method to be invoked. In particular, its getSource method returns the object that generated the event (e.g. the button or the text field). In order to find out what kind of object it is, we can use the instanceof operator. Replace the current body of the actionPerformed method with the following:
          if( e.getSource() instanceof JTextField ) {
    System.out.println (
    "That tickles my JTextField"
    } else {
    System.out.println (
    "That tickles my JButton"
  20. We'll eventually need to use the contents of the text field as the name of a new Acrobat. Add another statement for the case that the object generating the event was the text field that will display the value of the following string expression:
          "The Acrobat's name will be " + ((JTextField) e.getSource() ).getText()
  21. Question: Explain what happens if you add the same print statement for the other case.
  22. Make the JTextField a field, and replace ((JTextField) e.getSource() ) with the name of the field.
  23. Question: Explain why adding the print statement for the other case works now.
  24. Interacting Objects

  25. Our next step is to make the add button actually add an Acrobat to one of the lists. Add the following method to the AcrobatPanel class:
          public void addAcrobat( String theName ) {
    JButton acrobat = new JButton( theName );
    this.add( acrobat );
  26. Now replace the AcrobatPanel local variable in the constructor of the BasicGUIObjects class with a field, and add the following method:
          public void addAcrobat( String theName ) {
             basicPanel.addAcrobat( theName );
  27. Finally, add the following statement to the main method: mainFrame.addAcrobat( "FirstBasic" );
  28. Question: Does the new button appear? What if you resize the window?
  29. Add the following statement to the end of the addAcrobat method in the AcrobatPanel class: this.validate();
  30. Add a BasicGUIObjects field to the AddPanel class.
  31. Add a BasicGUIObjects parameter to the AddPanel constructor.
  32. Use the parameter to initialize the field.
  33. Modify the actionPerformed method so that it calls the addAcrobat method of the object's BasicGUIObjects field, using the contents of the text field as the parameter.
  34. Submitting the Project

  35. Make any final changes that you want to your project.
  36. Generate the HTML documentation for your project.
  37. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  38. Tag your CVS module BasicGUIObjects2.
  39. Summary

  40. Question: Describe the role of the addActionListener method.
  41. Question: Describe the role of the ActionListener interface.
  42. Question: Describe the role of the actionPerformed method.
  43. Question: Describe the role of the instanceof operator.
  44. Question: A new button should appear in the BasicAcrobat Objects panel when you click the add button, and the label of the button should be the contents of the text field. Explain in your own words the complete process that makes this happen.
  45. Question: How much time did you spend on Part 2 of BasicGUIObjects? Compare your answer to how much time you had estimated that you would spend. You receive full credit no matter how far off your estimate is!