130 likes | 325 Views
Event Handling. Feb 14, 2000. Event Model Revisited. Recall that a component can fire an event. Each type of event is defined as a distinct class. An event is received by one or more listeners .
E N D
Event Handling Feb 14, 2000
Event Model Revisited • Recall that a component can fire an event. • Each type of event is defined as a distinct class. • An event is received by one or more listeners. • An event listener is an object of a class which implements particular type of listener interface • Since a class can implement multiple interfaces, an object can act on multiple events of different types. • To receive an event of particular type, we only need to register with the component that fire the event.
Event Hierarchy java.util.EventObject java.awt.AWTEvent java.awt.event.ComponentEvent java.awt.event.WindowEvent java.awt.event.FocusEvent java.awt.event.ActionEvent java.awt.event.KeyEvent java.awt.event.ItemEvent java.awt.event.MouseEvent java.awt.event.TextEvent java.awt.event.AdjustmentEvent
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo implements ActionListener { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! public void actionPerformed( ActionEvent event) { String command = event.getActionCommand(); System.out.println(command); } public static void main(String args[]) { new ButtonDemo(); } } private void addButtons() { for (int i=0; i<10; i++) { JButton jb = new JButton(""+i); jb.addActionListener(this); pane.add(jb); } JButton jo = new JButton("+"); jo.addActionListener(this); pane.add(jo); jo = new JButton("-"); jo.addActionListener(this); pane.add(jo); jo = new JButton("*"); jo.addActionListener(this); pane.add(jo); jo = new JButton("/"); jo.addActionListener(this); pane.add(jo); jo = new JButton("AC"); jo.addActionListener(this); pane.add(jo); }
Different Ways to Handle Events • Directly implements a listener interface of interest. • Use an inner class that implements a listener interface of interest. • Use a listener adapter class, which is an abstract class. (We must override its methods) • Quite often, anonymous class will be useful!!! Anonymous class can be both listener inner class or listener adapter class.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! // add ButtonListener inner class here. public static void main(String args[]) { new ButtonDemo(); } } class ButtonListener implements ActionListener { public void actionPerformed( ActionEvent event) { String command = event.getActionCommand(); System.out.println(command); } } private void addButtons() { ButtonListener blt = new ButtonListener(); … … … Jbutton jo = new JButton("AC"); jo.addActionListener(blt); pane.add(jo); }
Listener Adapters • Usually we are not interested in all the methods defined in some listener interface. • However, to use an interface we need to implement all the methods in it. (Annoying!!!) • Adapters are abstract classes that already implements particular listener interface. • Actually no method in adapter class is an abstract method. Adapter class is just defined as abstract to force user to override particular method of interest.
void windowActivated(WindowEvent ev); void windowClosing(WindowEvent ev); void windowDeactivated(WindowEvent ev); … … … void windowOpened(WindowEvent ev); WindowListener Interface Overrides only one method of interest implements void windowActivated(WindowEvent ev) {} void windowClosing(WindowEvent ev){} void windowDeactivated(WindowEventev){} … … … void windowOpened(WindowEvent ev){} extends void windowClosing(WindowEent ev) { System .exit(0); } WindowAdapter class
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); Closer closer = new Closer(); frame.addWindowListener(closer); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! // add ButtonListener inner class here. // add Closer class here!!! public static void main(String args[]) { new ButtonDemo(); } } class Closer extend WindowAdapter { public void windowClosing( WindowEvent ev) { System.exit(0); } }
Shortcut to Use Adapters(Anonymous Class) … … ... public class ButtonDemo { Container pane; ButtonDemo() { … … … frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } } ) … ... } … … ... } No object handle is defined!!!
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintDemo { PaintDemo(){ JFrame frame = new JFrame("PaintDemo"); frame.setSize(400,300); DrawingCanvas canvas = new DrawingCanvas(); Container pane = frame.getContentPane(); pane.add(canvas,"Center"); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent evt) { System.exit(0); } } ); frame.setVisible(true); } // PaintDemo Continues public static void main(String args[]) { new PaintDemo(); } } // End of PaintDemo
class DrawingCanvas extends JPanel { int last_x,last_y; int cur_x,cur_y; DrawingCanvas() { setSize(getPreferredSize()); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent evt) { last_x = evt.getX(); last_y = evt.getY(); } } ); addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { cur_x = evt.getX(); cur_y = evt.getY(); Graphics g = getGraphics() g.drawLine(last_x,last_y, cur_x,cur_y); last_x = cur_x; last_y = cur_y; } } ); } }