Download Handling Window Events - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity! Web Design & Development Lecture 13 Docsity.com Event Handling part 3 Docsity.com Last Code Example • Problem – Interested in windowClosing method only – But have to provide definitions of all the methods, Why ? – Because a class implementing an interface has to provide definitions of all methods present in that interface. • Solution – Use Adapter classes Docsity.com Adapter Classes Docsity.com Adapter Classes • For listener interfaces containing more than one event handling methods, jdk defines adapter classes. Examples are – For WindowListener WindowAdapter – For MouseMotionListener MouseMotionAdapter – and many more • Adapter classes provides definitions for all the methods (empty bodies) of their corresponding Listener interface • It means that WindowAdapter class implements WindowListener interface. Docsity.com How to use Adapter Classes • Now our handler class will inherit from adapter class – public class EventsEx extends MouseMotionAdapter { …. } – Due to inheritance, all the methods of the adapter class will be available inside our handler class – Since adapter classes has already provided definitions with empty bodies. – We do not have to provide implementations of all the methods again – We only need to override our method of interest. Docsity.com Adapter Classes Adapter Class Listener (If Any) Registration Method ActionListener addActionListener AdjustmentListener addAdjustmentListener ComponentListener ComponentAdapter addComponentListener ContainerListener ContainerAdapter addContainerListener FocusListener FocusAdapter addFocusListener ItemListener addItemListener KeyListener KeyAdapter addKeyListener MouseListener MouseAdapter addMouseListener MouseMotionListener MouseMotionAdapter addMouseMotionListener TextListener addTextListener WindowListener WindowAdapter addWindowListener Docsity.com Example Code: Adapter Classes Modification of EventsEx // File EventsEx.java, Code listed in Handout section 13.1 …….. public class EventsEx extends WindowAdapter { JFrame f; JLabel coord; public void initGUI () { ……. // set layouts f.addWindowListener(this); ……. }//end initGUI Docsity.com Last Code Example • We have inherited from WindowAdapter • What if we want to use MouseMotionAdpater as well ? or what if our class already inherits form some other class ? • Problem – But, Java allows single inheritance • Solution – Use Inner Classes Docsity.com Inner Classes Docsity.com Inner Classes • A class defined inside another class Outer Class Inner class Docsity.com Inner Classes Handling Window Events Docsity.com Example Code: Inner Classes Handling Window Events // File EventsEx.java, Code listed in Handout section 13.2 …….. public class EventsEx { JFrame f; JLabel coord; //inner class private class WindowHandler extends WindowAdapter { // Event Handler for WindowListener public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } } // end of WindowHandler Docsity.com Example Code: Inner Classes Handling Window Events public void initGUI () { ……. // set layouts WindowHandler handler = new Window Handler (); f.addWindowListener(handler); ……. }//end initGUI // main method } // end of EventsEx class Docsity.com Example Code: Inner Classes Handling Window & MouseMotion Events // File EventsEx.java, Code listed in Handout section 13.3 …….. public class EventsEx { JFrame f; JLabel coord; //inner class private class WindowHandler extends WindowAdapter { // Event Handler for WindowListener public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } } // end of WindowHandler Docsity.com Example Code: Inner Classes Handling Window Events //inner class private class MouseHandler extends MouseMotionAdapter { // Event Handler for mouse motion events public void mouseMoved (MouseEvent me) { int x = me.getX(); int y = me.getY(); coord.setText(“Moved at [" + x + "," + y + "]” ); } } // end of MouseHandler Docsity.com Example Code: Inner Classes Handling Window Events public void initGUI () { ……. // set layouts WindowHandler wHandler = new WindowHandler (); f.addWindowListener(wHandler); MouseHandler mHandler = new MouseHandler(); f.addMouseMotionListener(mHandler); ……. }//end initGUI //main method } // end EventsExclass Docsity.com Anonymous Inner Classes • Has no name • Same as inner class in capabilities • much shorter • difficult to understand Docsity.com Named vs. Anonymous Objects • Named – String s = “hello”; System.out.println(s); – “hello” has a named reference s. • Anonymous – System.out.println(“hello”); Docsity.com Example Code: Anonymous Classes // File EventsEx.java, Code listed in Handout section 13.4 …….. public class EventsEx { JFrame f; JLabel coord; Docsity.com