project
play

Project Just when you thought it was safe! Event Driven - PDF document

Project Just when you thought it was safe! Event Driven Programming Project 2 has been released. Chat Room Threads Introduction GUIs Networking Minimum Submission due 11/3 CS3 - AWT/Swing 1 CS3 - AWT/Swing 2


  1. Project • Just when you thought it was safe! Event Driven Programming – Project 2 has been released. – Chat Room • Threads Introduction • GUIs • Networking – Minimum Submission due 11/3 CS3 - AWT/Swing 1 CS3 - AWT/Swing 2 Plan for this week Swing • Today: Intro to Events • So far we have covered how to build GUIs. • Tomorrow: Examples • The GUI will communicate with applications via Event Driven • Thursday: GUI Design Tips Programming. CS3 - AWT/Swing 3 CS3 - AWT/Swing 4 GUI Program Design Model-View-Controller • The GUI provides a view of the program, it is • The MVC pattern is commonly used to develop clearly not the program applications that have a GUI component • Making the GUI code as independent of the • Consists of three parts program code is a good strategy – Model – Changes in the program do not necessarily change the • The program GUI – View • The GUI – Different GUIs can be developed for the same program – Controller – Debugging and maintaining both the GUI and the program code is easier • The event handling mechanism CS3 - AWT/Swing 5 CS3 - AWT/Swing 6

  2. MVC MVC in Swing The model passes its data to the view for rendering Model View View Model The view determines which events The controller updates the model Are passed to the controller Based on the events received Program Logic Controller Controller The GUI The Application CS3 - AWT/Swing 7 CS3 - AWT/Swing 8 Event Driven Programming Event Handling • Programs respond to events that are generated • Events are represented by objects that gives outside the control of the program information about the event and identifies the event source – User types a key – The left mouse button is pressed – Event sources are typically components, but other kinds of objects can also be event sources – A CD is removed from the CD drive • A listener is an object that wants to be notified • When an event occurs, it is handled by an event when a particular event occurs handler – An event source can have multiple listeners registered • Event driven programming involves writing the on it handlers and arranging for the handler to be – A single listener can register with multiple event notified when certain events occur sources CS3 - AWT/Swing 9 CS3 - AWT/Swing 10 Listeners Events and Listeners • In order for an object to be notified when a particular event occurs, the object – Must implement the appropriate Listener interface – Be registered as an event listener on the appropriate event source CS3 - AWT/Swing 11 CS3 - AWT/Swing 12

  3. MVC in Swing Example • Buttons View – When pressed buttons will generate an ActionEvent myClass – This event generated will get broadcast to all ActionListener s registered with the button Program Logic • void actionPerformed (ActionEvent event) anotherClass Controller – Listener will respond appropriately. The GUI The Application CS3 - AWT/Swing 13 CS3 - AWT/Swing 14 Example PressMe public interface ActionListener extends public class PressMe implements ActionListener { EventListener public void actionPerformed(ActionEvent e) { { System.out.println ("I've been pressed"); } public void actionPerformed (ActionEvent e); public static void main (String args[]) { } JFrame win = new JFrame("Button Example"); JButton but = new JButton ("Press Me"); PressMe listener = new PressMe(); but.addActionListener (listener); win.getContentPane().add (but); win.pack(); win.show(); } } CS3 - AWT/Swing 15 CS3 - AWT/Swing 16 PressMe Events & Event Listeners • The event generated is passed to each listener • Listener can perform action based on the event. CS3 - AWT/Swing 17 CS3 - AWT/Swing 18

  4. PressMe2 PressMe2 public class PressMe2 implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println ("I've been pressed. My action is “ + e.getActionCommand()); } public static void main (String args[]) { JFrame win = new JFrame("Button Example"); JButton but; PressMe listener = new PressMe2(); for (int i = 0; i < 5; i++) { but = new Jbutton (“Button “ + i); but.addActionListener (listener); win.getContentPane().add (but); } win.pack(); win.show(); CS3 - AWT/Swing 19 CS3 - AWT/Swing 20 } } Questions? Anonymous Classes • An anonymous class is a local class that does not have a name. • An anonymous class allows an object to be created using an expression that combines object creation with the declaration of the class. • This avoids naming a class, at the cost of only ever being able to create one instance of that anonymous class. • This is handy in the AWT. CS3 - AWT/Swing 21 CS3 - AWT/Swing 22 Anonymous Class Syntax Using Anonymous Classes public class Dog { • An anonymous class is defined as part of a new private String breed; private String name; expression and must be a subclass or implement an public Dog( String theBreed, String theName ) { breed = theBreed; name = theName; interface } public String getBreed() { return breed; } new className ( argumentList ) { classBody } public String getName() { return name; } new interfaceName () { classBody } • The class body can define methods but cannot public int compareTo( Object o ) throws ClassCastException { Dog other = (Dog)o; define any constructors. int retVal = breed.compareTo( other.getBreed() ); if ( retVal == 0 ) • The restrictions imposed on local classes also retVal = name.compareTo( other.getName() ); apply return retVal; } } // Dog CS3 - AWT/Swing 23 CS3 - AWT/Swing 24

  5. Using Anonymous Classes Anonymous Class and Swing public void PrintDogsByName( List dogs ) { List sorted = dogs; • Anonymous classes are often used to define Collections.sort( sorted, simple listener behavior. new Comparator () { public int compare( Object o1, Object o2) { Dog d1 = (Dog)o1; Dog d2 = (Dog)o2; return d1.getName().compareTo( d2.getName() ); } ); Iterator i = sorted.iterator(); while ( i.hasNext() ) System.out.println( i.next() ); } CS3 - AWT/Swing 25 CS3 - AWT/Swing 26 PressMe3 Major AWT Listeners public class PressMe3 { Action Listener Type User clicks a button, presses return while ActionListener public static void main (String args[]) { typing in a text filed, or chooses a menu JFrame win = new JFrame("Button Example"); item JButton but = new JButton ("Press Me"); but.addActionListener Users closes a frame (main window) WindowListener (new ActionListener() { User presses a mouse button while the MouseListener public void actionPerformed(ActionEvent e) { System.out.println ("I've been pressed: Action " + cursor is over a component e.getActionCommand()); User moves the move over a component MouseMotionListener } }); A component becomes visible ComponentListener win.getContentPane().add (but); win.pack(); A component gets the keyboard focus FocusListener win.show(); } A table of list selection changes ListSelectionListener } CS3 - AWT/Swing 27 CS3 - AWT/Swing 28 More AWT Listeners Window Closing Action Listener Type • A very common event directed towards a window When the value of an adjustable is changed AdjustmentListener is a close event – The default behavior is to simply hide the JFrame For catching text events TextListener when the user closes the window User presses a key KeyListener • Normally we would want the program to terminate MouseWheelListener User moves the move wheel when the user closes the main window • Two steps required to accomplish this – Write an event handler for the close event that will terminate the program – Register the handler with the appropriate event source CS3 - AWT/Swing 29 CS3 - AWT/Swing 30

  6. WindowListener WindowAdapter • A class that implements WindowListener • The WindowListener interface – The methods in this class are empty. The class exists as – void windowActivated(WindowEvent e); convenience for creating listener objects. – void windowClosed(WindowEvent e); • To use the WindowAdapter class: – void windowClosing(WindowEvent e); – void windowDeactivated(WindowEvent e); – Extend this class to create a WindowEvent listener – void windowDeiconified(WindowEvent e); – Override the methods for the events of interest – void windowIconified(WindowEvent e); – Create a listener object using the extended class and – void windowOpened(WindowEvent e); then register it with the Window. • A class that implements WindowListener • When an event occurs the appropriate method in must implement all of these methods! the listener is invoked. CS3 - AWT/Swing 31 CS3 - AWT/Swing 32 PressMe4 Summary public class PressMe4 { • MVC public static void main (String args[]) { • Event Driven Programming JFrame win = new JFrame("Button Example"); JButton but = new JButton ("Press Me"); • Events and Swing ... win.addWindowListener( – Events new WindowAdapter() { public void windowClosing( WindowEvent e ) { – Listeners System.exit ( 0 ); } – Anonymous Classes }); ... win.pack(); – ActionListeners / WindowListeners win.show(); } } CS3 - AWT/Swing 33 CS3 - AWT/Swing 34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend