GUI/Swing Cheat Sheet

(back to the documentation page)

Jump To

General reminders

To display a Swing component, you must:

  1. Construct and initialize the component.
    Example: button = new JButton ("ButtonLabel");

  2. Add it to the content pane of the window or to a JPanel that is added to the display, and validate the content pane after adding all components. For example:

    Container contentPane = getContentPane();
    contentPane.add (button);
    ...
    contentPane.validate();
  3. Import javax.swing.* and often java.awt.* in the class creating the components.

To handle events from a GUI component, you must do the following:

  1. Declare that the class handling the event implements the appropriate listener interface.
    Example: implements ActionListener

  2. Define the method that the listener interface requires.
    Example: public void actionPerformed(ActionEvent event)

  3. Add a listener appropriate for the component to the component.
    Example: button.addActionListener(this);

  4. Import java.awt.event.* (and occasionally javax.swing.event.*) at the beginning of the class that is the listener.

When the listener method is called, you can find out which component sent the event by calling getSource() on the event:

public void actionPerformed (ActionEvent event) {
    Object theButton = event.getSource();
    if (theButton == framedCircleButton) {
        // Create a framed circle
    }
}

If a method returns a String or Object, remember to compare the result using the .equals() method, not ==. For example:
aMenu.getSelectedItem().equals("A value")

GUI Components

(back to top)

The following methods can be applied to any Component:

void setFont(Font f)
void setForeground(Color c)
void setBackground(Color c)

To construct a font use:

new Font(String name, int style, int size)

Find out the font names on the computer in Eclipse as follows:

  1. Open the Window menu
  2. Select Preferences
  3. Click on the triangle next to General, and then on the triangle next to Appearance
  4. Select Colors and Fonts
  5. In the colors and fonts panel, click on the triangle next to “Basic”.
  6. Select Text Font under Basic and then click on the “Change…” button. A dialog will appear that lists all of the fonts available.
  7. Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, or `Font.BOLD

The specific components we have considered:

Containers

(back to top)

Both JPanel and the object obtained by sending getContentPane() to a WindowController object are containers (and have type Container). The following methods are available for all containers. To define the type of layout, use:

void setLayout(LayoutManager lm)

LayoutManager may be any of the layout managers listed below. To add something to a container:

void add (Component c)

Component may be any Component (such as JButton, JTextField, JSlider, …) or Container (such as JPanel). Use the method above if the container has a FlowLayout or GridLayout. Use the one below if it has a BorderLayout.

void add (Component c, int position)

The position may be any of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, or BorderLayout.CENTER.

Construct a JPanel with new JPanel()

Layout Managers

(back to top)

  1. BorderLayout (Default for WindowController)

    Constructor:
    new BorderLayout()

  2. FlowLayout (Default for JPanel)

    Constructor:
    new FlowLayout()

  3. GridLayout

    Constructors:
    new GridLayout(int rows, int cols)
    new GridLayout(int rows, int cols, int colSpacing, int rowSpacing)