GUI Handling events CS 3331 1 Event Handling














- Slides: 14

GUI Handling events CS 3331 1

Event Handling v Mechanism to write control v Composed of: code Ä Event source Ä Event listener (or handler) 2

Handling events Event Handling (Cont. ) v Event ÄA way for GUI components to communicate with the rest of application Ä Implemented as event classes (e. g. , Action. Event) v Event source Ä Components generating events Examples: buttons, check boxes, combo boxes, etc. 3

Handling events Event Handling (Cont. ) v Event listener (or handler) Ä Objects Ä Must that receives and processes events implement an appropriate listener interface Ä Must inform the source its interest in handling a certain type of events (by registering) Ä May listen to several sources and different types of events 4

Handling events Example // create a button JButton button = new JButton(“Increment”); // register an action listener button. add. Action. Listener (new Button. Action. Listener()); // Action listener class Button. Action. Listener implements Action. Listener { public void action. Performed(Action. Event e) { // handle the event e … System. out. println(“Increment button pressed!”); } } 5

Handling events How Does It Work? h: Action. Listener b: JButton add. Action. Listener(h) <<create>> e: Action. Event action. Performed(e) get. Source() 6

Handling events Naming Convention v For event XYZ … Ä Event class: XYZEvent Ä Listener interface: XYZListener Ä Adapter class: XYZAdapter Ä Registration method: add. XYZListener() 7

Handling events Events and Listeners Event Listener Action. Event Component. Event Focus. Event Key. Event Mouse. Event Action. Listener Component. Listener Focus. Listener Key. Listener Mouse. Motion. Listener Window. Listener Item. Listener Text. Listener Window. Event Item. Event Text. Event … Adapter Component. Adapter Focus. Adapter Key. Adapter Mouse. Motion. Adapter Window. Adapter 8

Handling events Example: Resizing Component v To prevent windows from being resized too small, use Component. Event and Component. Listener public class Win. Java extends JFrame { public Win. Java(String name) { super(name); // …. set. Resizable(true); add. Component. Listener(Util. create. Component. Listener(400, 300); } // … } 9

Handling events Example (Cont. ) public class Util { public static Component. Listener create. Component. Listener(int width, int height) { return new My. Component. Listener(width, height); } private static class My. Component. Listener extends Component. Adapter { private int width, height; public My. Component. Listener(int w, int h) { width = w; height = h; } public void component. Resized(Component. Event e) { Component c = e. get. Component(); if (c. get. Width() < width || c. get. Height() < height) { c. set. Size(Math. max(width, c. get. Width()), Math. max(height, c. get. Height())); } } } // My. Component. Listener } 10

Handling events Using Anonymous Class // same code with anonymous class public class Util { public static Component. Listener create. Component. Listener( final int width, final int height) { return new Component. Adapter() { public void component. Resized(Component. Event e) { Component c = e. get. Component(); if (c. get. Width() < width || c. get. Height() < height) { c. set. Size(Math. max(width, c. get. Width()), Math. max(height, c. get. Height())); } } // component. Resized }; // Component. Adapter } // create. Component. Listener } 11

Handling events Exercise Write handler code by using anonymous class to print a goodbye message to System. out when the main window is closed. Hint: The Window. Listener interface defines, among others, the method “void window. Closing(Window. Event)”. public class Win. Java extends JFrame { public Win. Java(String name) { // …. // WRITE YOUR CODE HERE } // … } 12

Handling events Exercise Extend the counter applet to change its button color when the mouse enters the button. Hints - The interface Mouse. Listener declares, among others, void Mouse. Entered(Mouse. Event) and void mouse. Exited(Mouse. Event). - The method set. Background(Color) sets the background color of a widget. - The source (e. g. , JButton) can be obtained from an event object by calling the method “Object get. Source()”. 13

Handling events Exercise (Cont. ) public Counter. Applet extends Applet { public Counter. Applet() { // … JButton button = new JButton("Increment"); // WRITE YOUR CODE HERE! // … } 14