AWT/GUI Cheat Sheet

General reminders

To display an AWT component, you must:

To get input from an AWT component, you must do the following:

Finally, you must glue the display with the input handling method:

If a method returns a String, remember to compare the result using the equals method, not ==:

    aChoice.getSelectedItem ().equals ("A value")

GUI Components

The following methods can be applied to any Component:

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

The specific components we have considered:

  1. Font

    Construct a font using:

        new Font (String name, int style, int size)
    
    Find out the font names on the computer in CodeWarrior as follows:

    Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, or Font.BOLD + Font.ITALIC.

  2. Button
    Constructor:
    new Button(String s)
    
    GUI Methods:
    void addActionListener (ActionListener al)
    String getLabel( )
    void setLabel(String s)
    
    Listening and Event Methods:
    void actionPerformed(ActionEvent e)
    
    To find out which button was clicked, use:
    e.getSource()
    
  3. Checkbox
    Constructors:

    Independent checkbox:

    new Checkbox(String s, boolean selected)
    
    Radio button:
    new Checkbox(String s, boolean selected, CheckboxGroup cg)
    
    Create CheckboxGroup with
    new CheckboxGroup()
    
    GUI Method:
    void addItemListener(ItemListener il)
    
    Listening and Event Methods:
    void itemStateChanged(ItemEvent e)
    
    To find out which checkbox was clicked, use:
    e.getSource()
    
    To find out if the checkbox was checked (as opposed to cleared), use:
    e.getStateChange() == ItemEvent.SELECTED
    
  4. Choice
    Constructor:
    new Choice( )
    
    GUI Methods:
    void addItemListener(ItemListener il)
    
    void addItem(String item)
    
    To find out which item was selected, use:
    String getSelectedItem( )
    
    Listening and Event Methods:
    void itemStateChanged(ItemEvent e)
    
  5. Label
    Constructors:
    new Label(String s)
    
    new Label (String s, int align)
    
    align is one of Label.RIGHT, Label.LEFT, Label.CENTER
    GUI Methods:
    void setText(String s)
    
    String getText( )
    
    Listening and Event Methods:

    no listeners available for labels

  6. Scrollbar
    Constructor:
    new Scrollbar(int orientation, int value, int visible,
                  int minimum, int maximum)
    
    orientation is one of Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
    GUI Methods:
    addAdjustmentListener(AdjustmentListener al)
    
    void setValue(int newVal)
    
    To find out the current value, use:
    int getValue( )
    
    Listening and Event Methods:
    void adjustmentValueChanged(AdjustmentEvent e)
    
  7. TextField
    Constructors:
    new TextField(String s)
    
    new TextField(String s,int cols)
    
    GUI Methods:
    addActionListener(ActionListener al)
    
    void setText(String s)
    
    To find out the value typed, use:
    String getText( )
    
    Listening and Event Methods:
    void actionPerformed(ActionEvent e)
    

Containers

Both WindowController and Panel are containers. 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 Button, TextField, Slider, ...) or Container (such as Panel). 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.

Constructing a Panel is very straightforward:

new Panel()

Layout Managers

  1. BorderLayout (Default for WindowController)

    Constructor:

    new BorderLayout()
    
  2. FlowLayout (Default for Panel)

    Constructor:

    new FlowLayout()
    
  3. GridLayout

    Constructors:

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