Alan Kaminsky
•
Department of Computer Science
•
Rochester Institute of Technology
•
4486
+
2220
= 6706
Home Page
Advanced Programming
•
4003-707-02
•
Fall Quarter 2008
Course Page
4003-707-02 Advanced Programming
Module 5. GUIs -- Lecture Notes
Prof. Alan Kaminsky
Rochester Institute of Technology -- Department of Computer Science
The MVC Pattern
Model:
the core classes
View:
the GUI classes
Controller:
the stuff that hooks the core and the GUI together
Controller composed of
listeners
GUI listeners -- change core state in response to user input
Core listeners -- update GUI when core state changes
The processing loop for user actions
User does an input action (mouse click, key press, . . .)
AWT thread
calls a method on a GUI listener in the controller
GUI listener calls a method on a core object
Core object changes core state
Core object calls a method on a core listener in the controller
Core listener calls a method on the GUI object
GUI object updates GUI (repaint display, enable button, . . .)
Core initiated actions
Timer thread
calls a method on a core object
Core object changes core state
Core object calls a method on a core listener in the controller
Core listener calls a method on the GUI object
GUI object updates GUI (repaint display, enable button, . . .)
Java Swing
The window: Class javax.swing.JFrame
Widgets within the window held in a
container
For example, class javax.swing.JPanel
A container has a
layout manager
Determines where the widgets will be displayed within the container
For example, class javax.swing.BoxLayout
Many other layout managers available
Useful JPanel attributes
Border
Background color
Adding widgets to a JPanel with a BoxLayout
BoxLayout.X_AXIS: Widgets appear from left to right
BoxLayout.Y_AXIS: Widgets appear from top to bottom
A JPanel may contain other JPanels, to organize widgets hierarchically
Adding gaps between widgets
Examples of Java Swing widgets
Class javax.swing.JButton -- button
Class javax.swing.JTextField -- edit box
Class javax.swing.JSpinner -- edit box with up and down buttons
Class javax.swing.JComboBox -- box with drop-down menu
Class javax.swing.JLabel -- fixed label
Class javax.swing.JMenu, javax.swing.JMenuBar -- menus
Many others
JFrame
pack()
method
JFrame
setVisible()
method
Handling User Input with Java Swing
Java Swing itself uses the MVC pattern
Add listeners to widgets
Respond to an action on a widget
For example, a button was pressed
addActionListener()
method
Interface java.awt.event.ActionListener
Class java.awt.event.ActionEvent
Respond to a change within a widget
For example, a spinner's value went up or down
addChangeListener()
method
Interface javax.swing.event.ChangeListener
Class javax.swing.event.ChangeEvent
Respond to a mouse click (in a widget that doesn't do anything else with mouse clicks)
For example, mouse was clicked in a graphical output panel
addMouseListener()
method
Interface java.awt.event.MouseListener
Class java.awt.event.MouseAdapter
Class java.awt.event.MouseEvent
Respond to a keystroke (rather than let the widgets themselves handle it)
The keyboard focus
Class java.awt.KeyboardFocusManager
Class java.awt.KeyEventDispatcher
Class java.awt.event.KeyEvent
Respond to actions on the window (JFrame)
Interface java.awt.event.WindowListener
Class java.awt.event.WindowAdapter
Class java.awt.event.WindowEvent
Widget listeners delegating to GUI listeners
The processing loop for user actions, more detail
User does an input action (mouse click, key press, . . .)
AWT thread calls a method on the widget's listener
Widget listener calls a private method in the GUI object
Private method calls a method on a GUI listener in the controller
GUI listener calls a method on a core object
Core object changes core state
Core object calls a method on a core listener in the controller
Core listener calls a method on the GUI object
GUI method calls
repaint()
on the display
Core listener calls another method on the GUI object, maybe
GUI method calls
setEnabled()
on a button
At a later time, not part of the above chain of calls:
AWT thread notices a widget needs repainting
AWT thread calls widget's
paintComponent()
method
For class edu.rit.swing.DisplayablePanel,
paintComponent()
method calls Displayable object's
draw()
method
draw()
method draws stuff in DisplayablePanel's graphics context
Advanced Programming
•
4003-707-02
•
Fall Quarter 2008
Course Page
Alan Kaminsky
•
Department of Computer Science
•
Rochester Institute of Technology
•
4486
+
2220
= 6706
Home Page
Copyright © 2008 Alan Kaminsky. All rights reserved. Last updated 07-Oct-2008. Please send comments to ark
@
cs.rit.edu.