TopDebugging TipsSwing

Swing

As indicated in the textbook, adding graphic user interface (GUI) items to the screen is pretty easy. Here are the steps:

  1. Create the item and initialize it if necessary. E.g.,
    		figureMenu = new JComboBox();
    		figureMenu.addItem("FramedSquare");
    		figureMenu.addItem("FramedCircle");
    		figureMenu.addItem("FilledSquare");
    
  2. Add the items to the content pane of the WindowController, and validate the pane. E.g.,
    		Container contentPane = getContentPane();
    		contentPane.add(figureMenu, BorderLayout.SOUTH);
    		contentPane.add(colorMenu, BorderLayout.NORTH);
    		contentPane.validate();
    

In Java 5 you may add the components directly to the WindowController rather than first getting the content pane and adding to it. Thus in Java 5 you can write:

    this.add(figureMenu, BorderLayout.SOUTH);
    this.add(colorMenu, BorderLayout.NORTH);
    this.validate();
or even omit the this altogether.

A simple example is the program ComboBoxDrawing, which is a variant of the drawing program from last time where the buttons created by drawing rectangles on the screen have been replaced by menus, which are objects from class JComboBox, from the package javax.swing.


TopDebugging TipsSwing