Activity 6.4 - Constructing a frame containing a range of Swing components

Topic

In this activity we run a series of sample programs which illustrate a range of different visual components.

Materials

We have provided in this project a base class SwingClass that extends JFrame and a main class SwingClassTest that creates an instance of SwingClass and makes it visible.

In addition the Solution file contains code for the following classes:

where each successive class adds extra features to the previous one, and a class:

Instructions

  1. Run the project, setting ex6_4.SwingClassTest as the main class if prompted. You should see an empty window with the title 'User interface'.

  2. Now add the class SwingClass2 to the project, in the same package as the existing classes. Right-click on the package icon ex6_4 and choose New|Java Class..., name the new class SwingClass2, then copy the code for the class from the Solutions file. When you paste the code into SwingClass2, make sure that you do not accidentally overwrite the package declaration

    package ex6_4;
    which needs to be the first line in the class.

  3. Read the source code for SwingClass2. You will see that it extends the base class SwingClass, adding a menu bar.

    In order to see the modified interface you will need to modify SwingClassTest. Open this class and look for the line

    SwingClass testFrame = new SwingClass(); 

    Edit this to

    SwingClass testFrame = new SwingClass2(); 

    Note particularly that the variable testFrame is still of type SwingClass, although it is now made to reference an instance of the subclass SwingClass2.

    The variable testFrame is being used polymorphically. This works because an instance of a subclass can always be used where an instance of the parent class is expected. The subclass object will always be capable of responding to the same methods as an object of the parent class, although it may override them to add modified behaviour. This is exactly what happens here. When the setVisible method is invoked the SwingClass2 object displayed shows the original interface but with a menu bar added.

    Run the project again to see the modified interface.

  4. Now add SwingClass3 and MyCanvas to the project, following the same instructions as for SwingClass2. If you examine the code for SwingClass3 you will see that it further extends the user interface by including a text and a drawing area.

    Change the code in SwingClassTest so that this time an object of class SwingClass3 is assigned to the polymorphic variable testFrame.

    Run the program to see the frame with the new features added.

  5. Finally add SwingClass4. Change SwingClassTest so that this time it makes use of a SwingClass4 object. Run the project to see the final result, with menu bar, text area, drawing area, text field, button, checkbox and drop down list.

Notes

If you experiment you will find that the interface elements 'work', in the sense that they provide feedback if we manipulate them. For example the drop down list does drop down, the check box can be checked, you can type in the text area and so on. However this is as far as it goes at present, since we have not yet written any code to detect these actions and respond appropriately. How this is done will be covered in the next unit.