Download presentation
Presentation is loading. Please wait.
1
Lecture 15.1 Event Delegation
2
EventButton Revisited
import java.awt.event.*; import javax.swing.JButton; /** EventButton class (abstract) * Author: David Riley * Date: May, 2004 */ public abstract class EventButton extends JButton implements ActionListener { /** post: this is created with itself as an action listener * and getText() == s */ public EventButton(String s) { super(s); addActionListener(this); } public abstract void actionPerformed( ActionEvent e ); _____________ __________ ____________________
3
Why? (use abstract methods)
Abstract Class An abstract class is any class that (generally) contains one or more abstract method(s). Another name for an abstract class is deferred class. public abstract class MyClass { . . . public abstract void deferredMethod(); } Abstract methods can be void or non-void, have a parameter list or be parameterless, be public, private or protected. Note that AbstractButton class is abstract but contains no abstract methods. The idea is simply to create a class that cannot be instantiated. Why? (use abstract methods) 1) 2) 3)
4
Interface Why? (use interfaces)
An interface is a class (sort of) in which all methods are deferred. public interface MyInterface{ . . . public void deferredMethod(); } Note the syntax Interfaces may contain instance variables, but only if initialized, final, and static (stay tuned for static). Every method in an interface is implicitly deferred and public. Why? (use interfaces) Note that most interfaces do NOT include variables. 1) A class/interface "inherits" an interface using the word implements, instead of extends. Multiple interfaces can be inherited by the same class, but all deferred methods must be coded by the inheriting class.
5
javax.swing.JComponent
javax.swing.AbstractButton {abstract} «constructor» + AbstractButton() «update» + void addActionListener( java.awt.event.ActionListener ) . . . «interface» java.awt.event.ActionListener «event handler» + void actionPerformed( java.awt.event.ActionEvent ) javax.swing.JButton «constructor» + JButton() + JButton( String ) . . . EventButton {abstract} «constructor» + EventButton( String ) «event handler» + void actionPerformed( java.awt.event.ActionEvent )
6
Event Delegation In Java events can be delegated to any object, given two requirements: 1) The delegate must belong to a class that implements ???Listener. 2) Must execute: eventGenerator.add???Listener( theDelegate ); import java.awt.event.*; //ActionEvent and ActionListener import javax.swing.*; //JFrame and JButton public class Driver implements ActionListener{ private JFrame win; private JButton hiGrampsButton; public Driver() { win = new JFrame("Button Delegation"); win.setBounds(20, 20, 200, 220); win.setLayout(null); win.setVisible(true); hiGrampsButton = new JButton("Hi"); hiGrampsButton.setBounds(50, 50, 100, 100); hiGrampsButton.addActionListener(this); win.add( hiGrampsButton, 0); win.repaint(); } public void actionPerformed(ActionEvent e) { System.out.println( "Hi Gramps!" );
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.