Intermediate Java1 An example that uses inner classes Week Four Continued
Intermediate Java2 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
Intermediate Java3 Babysitting A baby in the home needs attention. Many events surrounding the baby are not easily ignored. Events can be of different types.
Intermediate Java4 One possible source of events
Intermediate Java5 Babysitting Before responding to an event, a babysitter typically takes note of the source and the type of event.
Intermediate Java6 Babysitting The sitter needs to know both the event type and the event source. Event source Event type Handler
Intermediate Java7 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.
Intermediate Java8 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();
Intermediate Java9 EventObject AWTEvent ActionEventComponentEvent InputEvent WindowEvent MouseEvent KeyEvent The Event Type is described by these Classes. Object String getActionCommand() int getX() char getKeyChar() boolean isAltDown()Window getWindow() public Object getSource();
Intermediate Java10 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.
Intermediate Java11 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).
Intermediate Java12 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.
Intermediate Java13 A mouse object A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked The listener object has methods that are called for particular events Event Source Event type object has data and methods Listener
Intermediate Java14 A mouse object A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked The listener object has methods that are called for particular events Implements MouseListener The event object is sent to a listener method A mouse object must be told who its listener is.
Intermediate Java15 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.
Intermediate Java16 Some of the Listener Interface Hierarchy EventListener ActionListener abstract void actionPerformed(ActionEvent e); MouseListener abstract void mouseClicked(MouseEvent e) KeyListener abstarct public void keyTyped(KeyEvent e)KeyEvent A tagging interface with no methods.
Intermediate Java17 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?
Intermediate Java18 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
Intermediate Java19 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.
Intermediate Java20 public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } Create a listener. Tell the applet who to call with any MouseEvent objects.
Intermediate Java21 Spying on the mouse Java Applets need an HTML file.
Intermediate Java22 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.
Intermediate Java23 …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…
Intermediate Java24 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 ActionEvent Object ActionEvent Object addActionListener() ActionListene r
Intermediate Java25 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)
Intermediate Java26 Hit the X and the program exits Once again but with a window
Intermediate Java27 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); }
Intermediate Java28 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) { } } But we have to implement ALL of the functions
Intermediate Java29 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!!
Intermediate Java30 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); }
Intermediate Java31 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); } ); } }