Lecture 9: Graphical User Interfaces and Events

What You Will Learn Today

  1. Use event handlers such as listeners and adapters to process mouse and keyboard input.
  2. Compare AWT and Swing user interfaces.
  3. Use GUI components such as buttons, text components, lists and menus.
  4. Use containers and layout managers to group and organize components.
  5. Describe advantages of visual programming.

Events

Handling Events

Listeners

MouseListener Interface

KeyListener Interface

Template for Applets with Listeners

class MyClass extends Applet { // an applet (could be an application)
   public MyClass() { // constructor
      class MyListener implements ListenerInterface // an inner class
         { public void eventOccurred(EventClass event) { /* add event action here */ } }
      MyListener listener = new MyListener();
      eventSource.addAListener(listener);
   }
   public void paint(Graphics g) { /* paint something here */ }
}

Adapters

AWT and Swing

Tips

Steps to Handle Events

  1. Implement the data that your program manipulates.
  2. Implement the visual representation of the data.
  3. Design a user interface for manipulating the data.
  4. Supply the user interface components.
  5. Supply event handler classes.
  6. Make listener objects and attach them to the event sources.

Graphical Components

java.awt javax.swing Description
Label JLabel displays a line of text; not directly editable by the user
TextField JTextField displays a line of text and can be edited by the user and used for input
TextArea JTextArea displays several lines of text; can be edited and used for input
Button JButton a rectangular push button that starts an action when clicked
Checkbox JCheckBox a small square button that can be selected or unselected
CheckboxGroup JRadioButton a group of radio buttons (small round buttons to select only one option)
List JList a scrolling list of selectable text items; click to select one or more
Choice   a drop-down list of text items; click to select one
MenuBar, Menu JMenuBar, JMenu used to create menus at the top of the frame
Scrollbar JScrollBar a vertical or horizontal slider with arrows at each end
Canvas   area for catching user events e.g. for drawing
  JComboBox combines a text box with a pull-down list of choices (for easier data entry)

Creating Graphical Components

Handling Events from Graphical Components

Containers

Layout Managers

A layout manager is an object which arranges components in a container.

Layout Manager Description
FlowLayout place elements left to right on one row; put on next row if not enough space (the easiest layout)
alignment can be FlowLayout.LEFT, CENTER or RIGHT
BorderLayout place elements in predefined areas; center area expands to fill extra space
areas are BorderLayout.NORTH, SOUTH, EAST, WEST, CENTER
GridLayout place elements in rows and columns; all elements take an equal amount of space
CardLayout place elements in the same space; show only one at a time to save space (e.g. tabbed window or photo browser)
GridBagLayout a layout which allows precise positioning (the most complex layout)
BoxLayout place elements in one row or one column (javax.swing)
OverlayLayout (javax.swing)

Using Layout Managers

Components are usually set up in the init() method of the applet.

Steps for Designing Layout Management

  1. Make a sketch of your desired component layout.
  2. Find groupings of adjacent components within the same layout.
  3. Identify layouts for each group.
  4. Group the groups together.
  5. Write the code to generate the layout.

Visual Programming

To Do After Class