310 likes | 322 Views
Learn how to handle events in Java using inner classes. This example focuses on event handling in GUI programming with AWT and Swing components.
E N D
Week Four Continued An example that uses inner classes 95-713 Intermediate Java
Event Handling • Used by the Abstract Window Toolkit (AWT) – for basic GUI • programming Java 1.0 • Used by Swing -- Better components than AWT Java 2 • Used by JavaBeans -- reusable software components, like • Visual Basic, that can be manipulated • in a builder tool 95-713 Intermediate Java
Babysitting A baby in the home needs attention. Many events surrounding the baby are not easily ignored. Events can be of different types. 95-713 Intermediate Java
One possible sourceof events 95-713 Intermediate Java
Babysitting Before responding to an event, a babysitter typically takes note of the source and the type of event. 95-713 Intermediate Java
Babysitting Handler Event type Event source The sitter needs to know both the event type and the event source. 95-713 Intermediate Java
Event Handling The window manager software may generate hundreds of different events. Examples include mouse clicks, mouse movements, key strokes, and timer ticks. A programmer is typically interested in a small subset of all of the possible events that may occur. 95-713 Intermediate Java
Events and Event Objects • Since events may be of different types but still exhibit some • shared traits (inheritance) Java represents the event classes • in a hierarchy. • The root class is called java.util.EventObject. The only common • feature shared by all events is a source object. So we find the • following method in the EventObject class : • public Object getSource(); 95-713 Intermediate Java
Object The Event Type is described by these Classes. EventObject public Object getSource(); AWTEvent ActionEvent ComponentEvent String getActionCommand() WindowEvent InputEvent boolean isAltDown() Window getWindow() MouseEvent KeyEvent int getX() char getKeyChar() 95-713 Intermediate Java
ComponentEvent • - A low-level event which indicates that a component moved, changed size, or changed visibility • For notification only • The AWT will automatically handle component moves and resizes internally so that GUI layout works properly regardless of whether a program is receiving these events or not. 95-713 Intermediate Java
ActionEvent • Indicates that a component-defined action occured • High-level event is generated by a component (such as a Button) • when the component-specific action occurs • (such as being pressed). 95-713 Intermediate Java
Event handling usually involves three types of objects • Objects are used to hold and report on information about the event. • Objects are typically the source of events. • Objects, called listeners, are used to handle events. 95-713 Intermediate Java
A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked Event type object has data and methods Event Source Listener The listener object has methods that are called for particular events 95-713 Intermediate Java
A mouse object must be told who its listener is. A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked Implements MouseListener The listener object has methods that are called for particular events The event object is sent to a listener method 95-713 Intermediate Java
The Listeners • There are different types of Listeners. • MouseListeners • WindowListeners • ScrollBarListeners • Etc.. • These Listeners are interfaces. Remember what an interface • provides? If class X implements an interface then class X promises • to provide (at least) the methods declared in the interface. 95-713 Intermediate Java
Some of the Listener InterfaceHierarchy A tagging interface with no methods. EventListener KeyListener ActionListener MouseListener abstract void actionPerformed(ActionEvent e); abstract void mouseClicked(MouseEvent e) abstarct public void keyTyped(KeyEvent e) 95-713 Intermediate Java
A Listener Interface public interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mouseReleased(MouseEvent e); } What does it mean if I claim that I implement this interface? 95-713 Intermediate Java
An Example of creating a listener // MouseSpyApplet.java import java.applet.Applet; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; Applets can be an event source 95-713 Intermediate Java
class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent event) { System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse entered. x = " + event.getX() + " y = " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse exited. x = " + event.getX() + " y = " + event.getY()); } public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed. x = " + event.getX() + " y = " + event.getY()); } public void mouseReleased(MouseEvent event) { System.out.println("Mouse released. x = " + event.getX() + " y = " + event.getY()); } } Since we are implementing the MouseListener interface we must provide all of these methods!! But, at least now we have a listener. 95-713 Intermediate Java
Create a listener. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } } Tell the applet who to call with any MouseEvent objects. 95-713 Intermediate Java
Java Applets need an HTML file. <html> <head> </body> </head> <B>Spying on the mouse</b><p> <applet code="MouseSpyApplet.class" width=400 height=200> </applet> </html> 95-713 Intermediate Java
Creating a Listener Another approach Suppose a friendly sole created this class: public class MouseAdapter implements MouseListener { void mouseClicked(MouseEvent e){} void mouseEntered(MouseEvent e){} void mouseExited(MouseEvent e){} void mouseReleased(MouseEvent e){} } Now, suppose we extend this class. What must we provide? Note the empty bodies. 95-713 Intermediate Java
…only those methods that we are interested in. The other methods, when called, do nothing. Java provides several classes called “Adapter” classes for this purpose. There are WindowAdapters, MouseMotionAdapters… 95-713 Intermediate Java
Register the listener Suppose we have a Button object Button b = new Button(); We must determine what events a particular component generates. We can see from the documentation that the Button class has an addActionListener() method. We resister the listener through the addActionListener method. Button b ActionListener addActionListener() ActionEvent Object 95-713 Intermediate Java
Register The Listener • We want to listen for the ActionEvent object coming from the button. • So, we tell the button what object will listen for the ActionEvent object: • b.addActionListener(sitter) 95-713 Intermediate Java
Once again but with a window Hit the X and the program exits 95-713 Intermediate Java
Create a WindowCloseSitter Class import javax.swing.*; import java.awt.event.*; public class CloseDemo { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); WindowCloseSitter h = new WindowCloseSitter(); f.addWindowListener(h); } } 95-713 Intermediate Java
But we have to implement ALL of the functions class WindowCloseSitter implements WindowListener { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } 95-713 Intermediate Java
Let’s use The WindowAdapter class public abstract class WindowAdapter implements WindowListener { public void windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } A built in class -- we already have the empty bodies!! 95-713 Intermediate Java
The Window again import javax.swing.*; import java.awt.event.*; public class CloseDemo2 extends WindowAdapter { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener(new CloseDemo2()); } public void windowClosing(WindowEvent e) { System.exit(0); } } 95-713 Intermediate Java
Again with anonymous classes import javax.swing.*; import java.awt.event.*; public class CloseDemo3 { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } } 95-713 Intermediate Java