Presentation is loading. Please wait.

Presentation is loading. Please wait.

95-713 Intermediate Java1 An example that uses inner classes Week Four Continued.

Similar presentations


Presentation on theme: "95-713 Intermediate Java1 An example that uses inner classes Week Four Continued."— Presentation transcript:

1 95-713 Intermediate Java1 An example that uses inner classes Week Four Continued

2 95-713 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

3 95-713 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.

4 95-713 Intermediate Java4 One possible source of events

5 95-713 Intermediate Java5 Babysitting Before responding to an event, a babysitter typically takes note of the source and the type of event.

6 95-713 Intermediate Java6 Babysitting The sitter needs to know both the event type and the event source. Event source Event type Handler

7 95-713 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.

8 95-713 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();

9 95-713 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();

10 95-713 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.

11 95-713 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).

12 95-713 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.

13 95-713 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

14 95-713 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.

15 95-713 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.

16 95-713 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.

17 95-713 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?

18 95-713 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

19 95-713 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.

20 95-713 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.

21 95-713 Intermediate Java21 Spying on the mouse Java Applets need an HTML file.

22 95-713 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.

23 95-713 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…

24 95-713 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

25 95-713 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)

26 95-713 Intermediate Java26 Hit the X and the program exits Once again but with a window

27 95-713 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); }

28 95-713 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

29 95-713 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!!

30 95-713 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); }

31 95-713 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); } ); } }


Download ppt "95-713 Intermediate Java1 An example that uses inner classes Week Four Continued."

Similar presentations


Ads by Google