Notes for users of the Swing version of objectdraw

Converting from AWT to Swing

If you have been using the AWT version of the library and wish to use the Swing version of the library then most examples will work unchanged (we changed the definitions of Controller and WindowController to have them extend JApplet rather than Applet). However programs using AWT GUI components from the standard Java library should be converted as follows:

  1. Most GUI components need to have a "J" added before their class names. E.g., JPanel, JButton, JLabel, etc. The Choice buttons are replaced by a JComboBox that is slightly different (see the appropriate chapter of the text). As mentioned above, you will not have to replace WindowController by JWindowController as the WindowController in the new library extends JApplet directly. In particular, NONE of the examples before the GUI chapter need to be changed in any way!

  2. Rather than adding components directly to a WindowController, you must instead add them to the content pane of a WindowController. Thus rather than writing:

    add(inputPanel, BorderLayout.NORTH);
    you must instead write:
    Container contentPane = getContentPane();
    contentPane.add(inputPanel, BorderLayout.NORTH);
    You do not need to do this with panels. That is a JButton can be added directly to a JPanel without any use of a content pane.

  3. Obviously, you will also have to import the swing library to classes that use Swing components:

       import javax.swing.*;
    Again, if you read the appropriate chapter of the Swing version of the text, you will see plenty of examples of this.

    One strategy in converting is to replace the import java.awt.* by import java.awt.Container (needed for the content pane) and import statements for any Java layout classes you may be using (e.g., BorderLayout, GridLayout, etc.). Then add import javax.swing.*.

    This will likely generate many error messages in your classes. Most can be fixed by modifying the GUI component classes (e.g., Button) by prefixing a "J" (e.g., JButton). Also don't forget to add the statement

        Container contentPane = get contentPane();
    to the begin method and to replace statements of the form this.add(...) or add(...) by contentPane.add(...).