- Forward


Event-Driven Programming in GUIs
An Introduction with Examples in Java (Swing)


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • Traditional Programs:
    • Start at the first line and proceed step-by-step
  • Event-Driven Programs:
    • Some objects generate events and other objects respond to them
The Focus of Event-Driven Design
Back SMYC Forward
  • Step 1:
    • Identify the events that can occur (e.g., mouse clicks, timing signals, key presses)
  • Step 2:
    • The classes that can generate events of different kinds (often called event generators)
  • Step 3:
    • The classes that need to respond to events of different kinds (often called event receivers)
Event Models
Back SMYC Forward
  • AWT:
    • Event bubbling
  • Swing:
    • Event listening
The Event Queue
Back SMYC Forward
  • Responsibility:
    • A central repository of events that ensures that everything happens in the right order
  • Other Participants:
    • Event generators add events to the back of the queue (a process known as posting)
    • Event receivers are sent events as they are removed from the front of the queue (a process that is known as firing or dispatching)
Threads
Back SMYC Forward
  • The Event Dispatch Thread:
    • Events are dispatched in a single thread called the event dispatch tread
    • The SwingUtilities javax.swing.SwingUtilities class has invokeAndWait() and invokeLater() methods that can be used to execute code in the event thread if necessary
  • Using Other Threads:
    • A SwingWorker javax.swing.SwingWorker object can be used to execute code out of the event thread if necessary
Using a JButton javax.swing.JButton
Back SMYC Forward
  • The Interface to Implement:
    • Implement ActionListener java.awt.event.ActionListener
  • What this Means:
    • Handle the event in the actionPerformed() method
Using a JButton javax.swing.JButton (cont.)
Back SMYC Forward

An Example Observer

javaexamples/usinggui/ButtonHandler.java
 
Using a JButton javax.swing.JButton (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/ButtonDriver.java
 
Using a JButton javax.swing.JButton (cont.)
Back SMYC Forward

A More Sophisticated Example

javaexamples/usinggui/AdvancedButtonDriver.java
 
Using a JCheckBox javax.swing.JCheckBox
Back SMYC Forward
  • One Way:
    • It extends JToggleButton javax.swing.JToggleButton
    • So, it behaves like a JButton javax.swing.JButton
  • Another Way:
    • Implement ItemListener java.awt.event.ItemListener
Using a JCheckBox javax.swing.JCheckBox (cont.)
Back SMYC Forward

An Example Observer

javaexamples/usinggui/CheckBoxHandler.java
 
Using a JCheckBox javax.swing.JCheckBox (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/CheckBoxDriver.java
 
Using a JRadioButton javax.swing.JRadioButton
Back SMYC Forward
  • Basics:
    • Use the same model as buttons
  • Coordination:
    • Use a ButtonGroup java.awt.ButtonGroup
Using a JRadioButton javax.swing.JRadioButton (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/RadioButtonDriver.java
 
Using a Text Component
Back SMYC Forward
  • Kinds:
    • JTextField javax.swing.JTextField
    • JTextArea javax.swing.JTextArea
  • ActionEvents:
    • ActionEvent is generated when [Enter] is pressed
  • DocumentEvents:
    • Most applications only need to respond to changes in the contents of the model
    • The model is a Document javax.swing.text.Document
    • So, the important interface is DocumentListener javax.swing.event.DocumentListener
Using a Text Component (cont.)
Back SMYC Forward

An Example Observer

javaexamples/usinggui/TextFieldHandler.java
 
Using a Text Component (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/TextFieldDriver.java
 
Using a JList javax.swing.JList
Back SMYC Forward
  • Some Issues:
    • Doesn't support scrolling directly (more on this later)
    • Interval selection is supported by the DefaultListSelectionModel javax.swing.DefaultListSelectionModel
  • Handling Events:
    • Implement ListSelectionListener javax.swing.event.ListSelectionListener
Using a JList javax.swing.JList (cont.)
Back SMYC Forward

An Example Observer

javaexamples/usinggui/ListHandler.java
 
Using a JList javax.swing.JList (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/ListDriver.java
 
Using a JComboBox javax.swing.JComboBox
Back SMYC Forward
  • What they Combine:
    • A JTextField javax.swing.JTextField and a JList javax.swing.JList
  • Implication:
    • Implement either ItemListener java.awt.event.ItemListener or ActionListener java.awt.event.ActionListener (or both)
Using a JComboBox javax.swing.JComboBox (cont.)
Back SMYC Forward

An Example Observer

javaexamples/usinggui/ComboBoxHandler.java
 
Using a JComboBox javax.swing.JComboBox (cont.)
Back SMYC Forward

An Example Subject

javaexamples/usinggui/ComboBoxDriver.java
 
Responding to JSlider Objects
Back SMYC Forward
  • The Event(s):
    • ChangeEvent
  • The Interface(s):
    • ChangeListener
  • The Method(s)/Message(s):
    • void stateChanged(ChangeEvent event)
Responding to JSlider Objects (cont.)
Back SMYC Forward
An Example Observer
javaexamples/usinggui/SliderHandler.java
 
Responding to JSlider Objects (cont.)
Back SMYC Forward
An Example Subject
javaexamples/usinggui/SliderDriver.java
 
Menus
Back SMYC Forward
  • The Class Hierarchy:
    • JMenuItem javax.swing.JMenuItem extends AbstractButton AbstractButton
  • Implication:
    • Implement ActionListener java.awt.event.ActionListener
There's Always More to Learn
Back -