CS I - AWT 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)

Construct a font using:

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

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

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

Class

Constructor

GUI Methods

Listening and Event Methods

Button

new Button(String s)
void addActionListener(ActionListener al)
String getLabel( )

void setLabel(String s)
void actionPerformed(ActionEvent e)

To find out which button was clicked, use:

e.getSource()

Checkbox

Independent checkbox:

new Checkbox(String s, 
             boolean selected)

Radio button:

new Checkbox(String s, 
             boolean selected,
	       CheckboxGroup cg)

Create CheckboxGroup with:

new CheckboxGroup()
void addItemListener(ItemListener il)
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()

Choice

new Choice( )
void addItemListener(ItemListener il)

void addItem(String item)

To find out which item was selected, use:

String getSelectedItem( )

void itemStateChanged(ItemEvent e)

Label

new Label(String s)
new Label (String s, int align)

align is one of Label.RIGHT, Label.LEFT, or Label.CENTER

no listeners available for labels

void setText(String s)
String getText( )

None

Scrollbar

new Scrollbar(int orientation,
              int value,
              int visible,
              int minimum,
              int maximum)


orientation is one of Scrollbar.HORIZONTAL or Scrollbar.VERTICAL

addAdjustmentListener
   (AdjustmentListener al)
void setValue(int newVal)

To find out the current value, use:

int getValue( )
void adjustmentValueChanged(AdjustmentEvent e)

TextField

new TextField(String s)

new TextField(String s,int cols)
addActionListener(ActionListener al)
void setText(String s)

To find out the value typed, use:

String getText( )
void actionPerformed(ActionEvent e)

Interfaces for Event Handlers

If a class implements one of the following interfaces, it must supply the methods listed below for that interface.

Interface

Methods

ActionListener

void actionPerformed(ActionEvent e)

AdjustmentListener

void adjustmentValueChanged(AdjustmentEvent e)

ItemListener

void itemStateChanged(ItemEvent e)

MouseListener

void mouseClicked(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)

MouseMotionListener

void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)

Events in Java

Sometimes you need access to information about the event that just happened in one of the event handler methods. All events support the method:

   Object getSource( )

This method returns the object that generated the event (e.g., the button that was pushed.)

Here are specialized methods available with some events:

Class

Methods

ActionEvent

No extra interesting methods

AdjustmentEvent

int getValue( )

ItemEvent

int getStateChange ()

Returns either ItemEvent.SELECTED or ItemEvent.DESELECTED

MouseEvent

Point getPoint( )
int getX( )
int getY( )
int getClickCount( )

Interfaces for Event Handlers

If a class implements one of the following interfaces, it must supply the methods listed below for that interface.

Interface

Methods

ActionListener

void actionPerformed(ActionEvent e)

AdjustmentListener

void adjustmentValueChanged(AdjustmentEvent e)

ItemListener

void itemStateChanged(ItemEvent e)

MouseListener

void mouseClicked(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)

MouseMotionListener

void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)

Events in Java

Sometimes you need access to information about the event that just happened in one of the event handler methods. All events support the method:

   Object getSource( )

This method returns the object that generated the event (e.g., the button that was pushed.)

Here are specialized methods available with some events:

Class

Methods

ActionEvent

No extra interesting methods

AdjustmentEvent

int getValue( )

ItemEvent

int getStateChange ()

Returns either ItemEvent.SELECTED or ItemEvent.DESELECTED

MouseEvent

Point getPoint( )
int getX( )
int getY( )
int getClickCount( )

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.

Class

Constructors

Panel

new Panel()

Layout Managers

Class

Constructors

BorderLayout
Default for WindowController

new BorderLayout()

FlowLayout
Default for Panel

new FlowLayout()

GridLayout

new GridLayout(int rows, int cols)

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