You are on page 1of 7

Java Foundation Classes Swing

• JFC: • Portable API:


Java Swing, Events – Abstract Window Toolkit (AWT) – The appearance and behavior (look-and-
– original user interface toolkit
feel) of the user interface components are
implemented in Java …
Readings: – don’t go there!

Just Java 2: Chap 19 & 21, or – Swing


– might work slightly differently from any host
– package javax.swing.*, introduced in Java 1.2
Eckel’s Thinking in Java: Chap 14 platform

– pluggable look-and-feels
Slide credits to CMPUT 301, Department of e.g., Motif, windows,…
Computing Science
University of Alberta 2 3

Containment Hierarchy Containment Hierarchy Swing

• Top-level container: • Atomic components: • Components


– place for other Swing components to paint – self-sufficient components that present and containers:
themselves information to and get input from the user – superclasses
– e.g., JFrame, JDialog, Japplet – e.g., JButton, JLabel, JComboBox, and interfaces
• Intermediate container: JTextField, JTable – extends
and implements
– simplify positioning of atomic components
– e.g., JPanel, JSplitPane, JTabbedPane

4 5 © O’Reilly 1999 6

1
Swing Containers Top-Level Containers
• Java • Notes: • JFrame example:
Documentation: – Container objects group components, – contains a single component JRootPane, which
– http://java.sun.com/j2se/1.4.
1/docs/api/javax/swing/pack arranging them for display with a layout has a JMenuBar (optional) and a content pane
age-summary.html
manager. – theFrame.setJMenuBar( theMenuBar )
• SwingSet: – theFrame.setContentPane( thePanel )
– http://java.sun.com/products
/javawebstart/demos.html
– add non-menu components to this content
pane
• Quick tutorial: – theFrame.getContentPane().add( aButton )
– http://java.sun.com/docs/bo
oks/tutorial/uiswing/start/swi
ngTour.html
7 8 9

Events Events Events

• Two approaches to event handling • Swing: • Swing:


– read-evaluation loop (client-written loop) – objects communicate by “firing” and – different event sources produce different
– notification-based (callbacks) “handling” events (event objects) kinds of events
• Swing uses the 2nd approach – (conventional method call)
e.g., a JButton object, when clicked,
generates an ActionEvent object, which is
– events are sent from a single source object handled by an ActionListener (an object
to one or more registered listener objects whose class implements this interface)

10 11 12

2
Events Events Event Handling
• class MyListener implements ActionListener {
• Handling: …
public void actionPerformed( ActionEvent event ) {
listener interface
– create a component // react to event
– e.g., a JButton …
}
– add it to the GUI }
– e.g., to a JPanel • …
– register a listener to be notified when the // instantiate event listener
1..* 1..* ActionListener listener = new MyListener();
event source event listener
component generates an event …
// instantiate event source
– e.g., interface ActionListener JButton button = new JButton( “Hello” );

– define the callback method // register event listener with event source
– e.g., actionPerformed() button.addActionListener( listener );

13 14 15

UML Sequence Diagram Event Handling Event Handling



:MyPanel
• Options for implementing listeners: • Listener class:
new
– listener class
:JButton
– anonymous inner classes
new
:MyListener – named inner classes
addActionListener ()

actionPerformed ()

16 17 18

3
Event Handling Event Handling Event Handling

• Anonymous inner listener class: • Named inner listener class: • Note:


– A class could potentially be both an event
source and event listener.

– Good or bad idea? …

19 20 21

Event Handling Dependencies Dependencies


• public class MyButton extends JButton implements
ActionListener { • Problems:

public MyButton() { – need to maintain consistency in the views (or
… observers)
addActionListener( this );
} – need to update multiple views of the common

public void actionPerformed( ActionEvent event ) { data model (or subject)

}
} – need clear, separate responsibilities for
• JButton button = new MyButton() …
presentation (look), interaction (feel),
computation, persistence

22 23 24

4
Model/View/Controller Model/View/Controller Model/View/Controller

• MVC roles: • MVC roles:


– model – view
– complete, self-contained representation of – tracks what is needed for a particular perspective
of the data
object managed by the application
e.g., bar chart view
e.g., spreadsheet document
– presentation issues
– provides a number of services to manipulate
the data – controller
e.g., recalculate, save – gets input from the user, and uses appropriate
information from the view to modify the model
– computation and persistence issues e.g., get slider value, trigger chart modify
–… – interaction issues

25 26 27

Model/View/Controller Model/View/Controller Model/View/Controller

• Separation: • In Swing: • In Swing:


– you can modify or create views without – in practice, views and controllers are – still, try to separate the model and its
affecting the underlying model implemented with Swing components and services so that it is Swing-free
listeners
– the model should not need to know about – model is like a “virtual machine” or “kernel”
all the kinds of views and interaction styles – both views and controllers will be specific to the application
available for it dependent on Swing APIs

– separate threads?

28 29 30

5
Model/View/Controller Model/View/Controller Pluggable Look-and-Feel

• Smalltalk: • Java and Swing: • Swing:


– originated the MVC concept – concept is still valid to help structure – the look-and-feel is implemented in Java, but
interactive applications could mimic Windows, Motif, Classic, Aqua,
e.g., use a framework that supports MVC etc.
– integral support in interactive applications
with MVC classes
– UIManager.setLookAndFeel(
– Swing internally uses a variant of MVC for “com.sun.java.swing.plaf.windows.WindowsLookAndFeel”
its pluggable look-and-feel capability … );

– UIManager.setLookAndFeel(
“javax.swing.plaf.metal.MetalLookAndFeel”
);

31 32 33

Pluggable Look-and-Feel Pluggable Look-and-Feel Pluggable Look-and-Feel

• SwingSet: • Idea: • Swing internals:


– http://java.sun.com/products/javawebstart/demos.html
– similar to skins, themes, schemes, etc., but – each component uses a user interface
must include “feel” as well as “look” delegate object (responsible for view and
controller roles)

component

model user interface user interface


object object manager

34 35 36

6
Pluggable Look-and-Feel Model/View/Controller

• Swing internals: • CRC cards for MVC:


– each component specifies a model – discuss what models, views, and
interface that an associated model class controllers there are in the system
must implement
– be a design critic

37 38

You might also like