GUIs in Java Swing, Events CS2110, SW Development Methods Readings:

advertisement
GUIs in Java
Swing, Events
CS2110, SW Development Methods
Readings:
MSD, Chapter 12
Lab Exercise
Why Study GUIs in CS2110
• First, why not?
– Complex topic, complex library
– Many classes, methods
– Hard to do this well initially
• Reasons we study GUIs
– Again, example of inheritance in a
framework, software reuse, etc.
– Event-driven programming
– An important form of program-control
2
Swing
• Swing is a Java library (framework) for
creating GUIs
– Part of a larger JFC (Java Foundation
classes)
– Replaces but uses an older library, AWT
– Another, newer alternative: SWT
– Used in Eclipse
• Swing apps will use look-and-feel of the
system they’re running on
– Or can be set by the program
3
Learning Swing
• Important things to learn
– Swing components
– E.g. buttons, text-fields, frames, etc.
– How to organize and arrange them
– Containers, layout managers
– How to make things change when
something happens
– Event-based programming
4
Containment Hierarchy
• Top-level container:
– place for other Swing components to paint
themselves
– e.g., JFrame, JDialog, Japplet
• Intermediate container:
– simplify positioning of atomic components
– e.g., JPanel, JSplitPane, JTabbedPane
5
Components and Containers
• See pages 809-816 in text
• All Swing GUI objects are Components
• Some are also Containers
– Example: JFrame, JPanel, etc
• You place other Components inside
Containers
– Example: a JFrame has buttons, text-fields, etc.
– Example: a JPanel is part of a window, in which
we organize GUI components
6
What We Do with Containers
• Add components to them
• Determine how these items will be
arranged on the screen
– Layout control
– We associate a Swing layout-manager with
each container
• Layout is very hard to do at the
beginning
– So we won’t sweat it in CS2110
7
Non-Containers
• Atomic components:
– self-sufficient components that present
information to and get input from the user
– e.g., JButton, JLabel, JList, JComboBox,
JTextField, JTable
8
Swing
• Components
and containers:
– superclasses
and interfaces
– extends
and implements
© O’Reilly 1999
9
Top-Level Containers
• JFrame example:
– contains a single component JRootPane, which
has a JMenuBar (optional) and a content pane
– add non-menu components to its content panel
– theFrame.add( aButton )
– Pre Java 5.0
– theFrame.getContentPane().add( aButton )
10
Events and User Interaction
• Programs so far:
main() is called and runs to completion
• Swing programs:
– “main” class has (or may be) a JFrame
– main() sets up the GUI and ends
– Program keeps running, waiting for and
responding to “events”
– Button pressed, text typed, window closed, etc
– Program is event-driven
11
Demo
12
Events
• Two approaches to event handling
– read-evaluation loop (client-written loop)
– notification-based (callbacks)
• Swing uses the 2nd approach
13
Events
• Swing:
– objects communicate by “firing” and
“handling” events (event objects)
– (conventional method call)
– events are sent from a single source object
to one or more registered listener objects
14
Events
• Swing:
– different event sources produce different
kinds of events
e.g., a JButton object, when clicked,
generates an ActionEvent object, which is
handled by an ActionListener (an object
whose class implements this interface)
15
MSD book, p. 837
16
Events
• Handling:
– create a component
– e.g., a JButton
– add it to the GUI
– e.g., to a JPanel
– register a listener to be notified when the
component generates an event
– e.g., interface ActionListener
– define the callback method
– e.g., actionPerformed()
17
Event Handling
• class MyListener implements ActionListener {
…
public void actionPerformed( ActionEvent event ) {
// react to event
…
}
}
• …
// instantiate event listener
ActionListener listener = new MyListener();
…
// instantiate event source
JButton button = new JButton( “Hello” );
…
// register event listener with event source
button.addActionListener( listener );
18
Common Technique (e.g. Jigloo)
• Use anonymous class to create a
listener object “on the fly”
// register event listener with event source
button1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
handlerForButton1Press(evt); //see below
}
});
... // and later you write the handler method
public void handlerForButton1Press(evt) {
// your code to handle button press
}
19
What’s this? Java Anonymous
Classes
• There’s a Java technique called anonymous
classes
– One of several types of nested class definition
– You’ll very often see it in GUI programming (Swing) and
with threads
• Situation:
– Sometimes Java’s design encourages us to create some
thing that might be used just once
– That thing needs to be wrapped up in a class, say because
we need a function object
• What couldn’t we just declare it at the same place
we use it? Why create a separate file?
Creating and Using an Anonymous Class
• Example: sort a list of Strings by their length
Collections.sort ( stringList, new Comparator() {
public int compare( Object o1, Object o2 ) {
return ((String) o1).length() - ((String) o2).length();
}
});
• We’ve created a new Comparator “on the fly”
– new creates a new instance, but what kind?
– Some object that implements Comparator
– Object not named, and its “true” class not named!
– What must a Comparator have? compare()
– We defined it right here, where it’s used!
Anonymous Classes: Comments
• Anonymous classes are unlike other classes
– They have no name
– Typically only implement methods in their interface or
superclass. No new methods!
– Since they have no name, can only define and use them at
one point in your code!
• Hard to understand at first? Sure!
– Naming an abstraction is important for human
understanding!
– Sorting, a Collection, Comparing
• Advice
– Keep them very short (and simple)!
– Be ready to understand them when you see them in Swing
and with threads
Download