Transcript ppt
Advanced Java Class
Events
Events
•
•
•
•
change in state initiated by system or user
java.util.EventObject
java.awt.event
java.swing.event
Event flow of control
• Listener registers with object that emits events.
– frame.addKeyListener( myKeyListener )
• An object emits an event
– for example, the user presses a key
• The JVM notifies all listeners registered with that object.
– for example, the method
public void keyPressed(KeyEvent e)
in the myKeyListener object is called when a user presses a key.
• The Listener extracts information from the Event object
and does something appropriate
– for example, it queries the Event to find out which key was
pressed, and then moves a frog in that direction.
Common AWT Events
ActionEvent
AdjustmentEvent
ComponentEvent
ContainerEvent
FocusEvent
ItemEvent
KeyEvent
MouseEvent
TextEvent
WindowEvent
A component such as a toolbar or menu item
emitted an action event (i.e. “cut “)
Scrollbar moved
Component moved, resized, etc.
contents of container changed
component gained or lost focus
selection changed in checkbox, list, etc.
key pressed, released, or typed
mouse entered, exited, clicked, pressed, or
released
Text edited
Window opened, closing, closed,
(de)activated, or (de)iconified
Common Swing Events
ChangeEvent
Very general -- something changed
ListSelectionEvent
A different item in a list was selected
– emitted by ListSelectionModel,
listened for by Jtable (& others)
A menu was selected, deselected, or
canceled.
Something in a TableModel changed
A different path in a tree was
selected – emitted by
TreeSelectionModel
MenuEvent
TableModelEvent
TreeSelectionEvent
Listeners & Adapters
• Listeners register with objects that emit events,
by calling a specific method:
add[fillInType]Listener(thisListener)
• One Listener class per Event Class
• Adapters are simple classes that provide empty
implementations of the methods.
• Use adaptors if you don’t want to implement all
the methods in a listener interferer
• Some Adapters implement more than one
Listener, and not all Listeners have Adapters
Example of Using Events
component.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e) {
System.out.println("key typed“
+e.getKeyChar());
}
});
ActionEvents
Actions
• Actions are a shortcut for ActionListeners
• They have names and (optionally) icons
• They can be added to tool bars and menu items.
• Example:
JMenuItem crashItem = new JMenuItem();
crashItem.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Audio.crash();
}
});
Actions and Events Task
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.TimerTask;
public class EventTask {
private static ImageDisplayer displayer;
public static void main( String[] args ){
JFrame frame = setUpFrame();
setUpMenuBar( frame );
setUpImageDisplayer( frame.getContentPane() );
frame.show();
}
private static void setUpImageDisplayer(
Container contentPane )
{
/*
fill in to put an instance of ImageDisplayer into the GUI
*/
}
private static JFrame setUpFrame() {
/*
fill in
set up a new 300 by 300 frame
tell it to respond to a user pressing the letter f by
asking the ImageDisplayer
to briefly show it’s image.
*/
}
private static void setUpMenuBar( JFrame frame ) {
/*
give the frame a menubar, with one menu,
which should have one menu item
the menu’s name should be “Image”, and the menu
item’s name should be “Show Image”
when the user selects the menu item, it should ask
the ImageDisplayer to briefly show it’s image
*/
}
static class ImageDisplayer extends JComponent {
//use definition from GUI task
}
}
Solution to task
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.TimerTask;
public class EventTask {
private static ImageDisplayer displayer;
public static void main(String[] args ){
JFrame frame = setUpFrame();
setUpMenuBar(frame);
setUpImageDisplayer(frame.getContentPane());
frame.show();
}
private static JFrame setUpFrame() {
JFrame frame = new JFrame("Event Task");
frame.setSize(300, 300);
frame.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'f') {
displayer.showImageBriefly();
}
}
});
return frame;
}
private static void setUpMenuBar(JFrame frame) {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Image");
JMenuItem showImageItem = new JMenuItem();
showImageItem.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
displayer.showImageBriefly();
}
});
showImageItem.setText("Show Image");
menu.add(showImageItem);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
}
private static void setUpImageDisplayer(Container
contentPane) {
displayer = new ImageDisplayer();
contentPane.add(displayer);
}
static class ImageDisplayer extends JComponent {
// use defiinition from gui1 task
}
}