1 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Events Gestione degli eventi.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Podsumowanie Nguyen Hung Son Uniwersytet Warszawski.
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Servlets. Applets Special Java programs (without a main) callable from HTML and executed in a graphic context. They can be executed by: a Java enabled.
J0 1 Marco Ronchetti - Servlets e JSP.
Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
CS 142 Lecture Notes: EventsSlide 1 Access to Event Object ● event variable (HTML): ● Passed as argument to function (DOM/Firefox): element.onclick = mouseClick;
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
CSE 341, S. Tanimoto Java-PolyDraw- 1 A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas.
TCU CoSc Programming with Java Handling Events.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Jan Event Handling Yangjun Chen Dept. Business Computing University of Winnipeg.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
Event Handling Events and Listeners Timers and Animation.
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
Form’s Events ISYS 562. Form Events Opening & closing form:Open, Close, Load, Unload Moving to and from the form: Activate, deactivate Moving from control.
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
Week 3 C For Win Lecture :.  Mouse Message  Keyboard Message.
1 Lecture 5: Conventional Input Models for Window Managers and Toolkits Brad Myers Advanced User Interface Software © Brad Myers.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Lecture Set 13 Drawing Mouse and Keyboard Events Part B – Mouse and Keyboard Events.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
The Web Wizard’s Guide To DHTML and CSS Chapter 6 Understanding Events.
1 Actionscript for Flash by Dr SC Li. 2 Understanding more about instances Symbolsgraphics buttons Movie clips Instances (without names) No interaction.
COSC 4126 User Interaction User Interaction capturing and responding to input events.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
MIS333k(Poynor) Chapter 2. Intro to VBA Events Where to plug in your programs?
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
Host Objects: Browsers and the DOM
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 6: Events in Java 1.0 Model –Event class –selecting component to handle –handleEvent method –helper methods (action, keyUp, mouseUp, etc.) 1.1.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Dr. Abraham Professor UTPA Graphical User Interface.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Computer Science 209 Graphics and GUIs.
Interfaces.
JavaScript and Events.
Visual Basic Properties, Methods and Events
Working with the Event Model
Web Design & Development Lecture 12
Introduction to jQuery
Presentation transcript:

1 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Events Gestione degli eventi

2 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Achieving OS Independence AWT Button Lightweight Button Peer Button Peer Motif Button OS Swing Button

3 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Modello 1.0 Ricorda: un Container è un Component!

4 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti handleEvent() public boolean handleEvent(Event evt) { switch (evt.id) { case Event.MOUSE_ENTER:return mouseEnter(evt, evt.x, evt.y); case Event.MOUSE_EXIT:return mouseExit(evt, evt.x, evt.y); case Event.MOUSE_MOVE:return mouseMove(evt, evt.x, evt.y); case Event.MOUSE_DOWN:return mouseDown(evt, evt.x, evt.y); case Event.MOUSE_DRAG:return mouseDrag(evt, evt.x, evt.y); case Event.MOUSE_UP: return mouseUp(evt, evt.x, evt.y); case Event.KEY_PRESS: case Event.KEY_ACTION:return keyDown(evt, evt.key); case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: return keyUp(evt, evt.key); case Event.ACTION_EVENT:return action(evt, evt.arg); case Event.GOT_FOCUS: return gotFocus(evt, evt.arg); case Event.LOST_FOCUS:return lostFocus(evt, evt.arg); } return false; } Questo codice fa parte del sorgente di AWT

5 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Event (1.0) /* Modifier constants */ public static final int SHIFT_MASK= 1 << 0; public static final int CTRL_MASK= 1 << 1; public static final int META_MASK= 1 << 2; ublic static final int ALT_MASK= 1 << 3; /* Action keys */ public static final int HOME= 1000; ublic static final int END= 1001; public static final int PGUP= 1002;... public static final int PAUSE= 1024; public static final int INSERT= 1025; /* Non-action keys */ public static final int ENTER= '\n'; public static final int BACK_SPACE= '\b'; public static final int TAB= '\t'; public static final int ESCAPE= 27; public static final int DELETE= 127; Questo codice fa parte del sorgente di AWT

6 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Event (1.0) /* Base for all window events. */ private static final int WINDOW_EVENT = 200; public static final int WINDOW_DESTROY = 1 + WINDOW_EVENT; public static final int WINDOW_EXPOSE = 2 + WINDOW_EVENT; public static final int WINDOW_ICONIFY= 3 + WINDOW_EVENT; public static final int WINDOW_DEICONIFY= 4 + WINDOW_EVENT; public static final int WINDOW_MOVED= 5 + WINDOW_EVENT; /* Base for all keyboard events. */ private static final int KEY_EVENT = 400; public static final int KEY_PRESS = 1 + KEY_EVENT; public static final int KEY_RELEASE = 2 + KEY_EVENT; public static final int KEY_ACTION = 3 + KEY_EVENT; public static final int KEY_ACTION_RELEASE= 4 + KEY_EVENT; /* Base for all mouse events. */ private static final int MOUSE_EVENT = 500; public static final int MOUSE_DOWN = 1 + MOUSE_EVENT; public static final int MOUSE_UP = 2 + MOUSE_EVENT; public static final int MOUSE_MOVE = 3 + MOUSE_EVENT; public static final int MOUSE_ENTER = 4 + MOUSE_EVENT; public static final int MOUSE_EXIT = 5 + MOUSE_EVENT; public static final int MOUSE_DRAG = 6 + MOUSE_EVENT; Questo codice fa parte del sorgente di AWT

7 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Event (1.0) /* Scrolling events */ private static final int SCROLL_EVENT = 600; public static final int SCROLL_LINE_UP= 1 + SCROLL_EVENT; public static final int SCROLL_LINE_DOWN= 2 + SCROLL_EVENT; public static final int SCROLL_PAGE_UP= 3 + SCROLL_EVENT; public static final int SCROLL_PAGE_DOWN= 4 + SCROLL_EVENT; public static final int SCROLL_ABSOLUTE= 5 + SCROLL_EVENT; public static final int SCROLL_BEGIN= 6 + SCROLL_EVENT; public static final int SCROLL_END= 7 + SCROLL_EVENT; /* List Events */ private static final int LIST_EVENT = 700; public static final int LIST_SELECT= 1 + LIST_EVENT; public static final int LIST_DESELECT= 2 + LIST_EVENT; /* Misc Event */ private static final int MISC_EVENT= 1000; //This event indicates that the user wants some action to occur public static final int ACTION_EVENT= 1 + MISC_EVENT; public static final int LOAD_FILE= 2 + MISC_EVENT; public static final int SAVE_FILE= 3 + MISC_EVENT; public static final int GOT_FOCUS= 4 + MISC_EVENT; public static final int LOST_FOCUS= 5 + MISC_EVENT; Questo codice fa parte del sorgente di AWT

8 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Dealing with events-1.0 import java.awt.*; import java.awt.event.*; class Applicazione extends Frame { int lastx, lasty; Graphics g; Button clear_button; public static void main(String s[]) { new Applicazione();} Applicazione() { setSize(200,200); clear_button = new Button("Clear"); add(clear_button,BorderLayout.NORTH); show(); g=getGraphics(); //dopo la show! } /** Respond to mouse clicks */ public boolean mouseDown( Event e, int x, int y) { lastx = x; lasty = y; return true; } /** Respond to mouse drags */ public boolean mouseDrag( Event e, int x, int y) { g.setColor(Color.black); g.drawLine(lastx, lasty, x, y); lastx = x; lasty = y; return true; } /** Respond to key presses */ public boolean keyDown( Event e, int key) { if ((e.id == Event.KEY_PRESS) && (key == 'c')) { clear(); return true; } else return false; } /** Respond to Button clicks */ public boolean action( Event e, Object arg) { if (e.target == clear_button) { clear(); return true; } else return false; } /** convenience method to erase the scribble */ public void clear() { g.setColor(this.getBackground()); g.fillRect(0, 0, bounds().width, bounds().height); }

9 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Eventi 1.1 Nel modello di Java 1.0 a ciascuna componente vengono notificati gli eventi che la riguardano, ed e’ poi sua responsabilità gestirli (eventualmente scalandoli nella gerarchia di contenimento). Il modello di Java 1.1 invece “centralizza” la gestione degli eventi facendo riferimento a delle classi “Listener”, presso le quali chi genera eventi deve registrarsi. La gestione dell’evento e’ responsabilità del listener. A volte, per semplicità, e’ possibile unificare il generatore ed il gestore degli eventi. Anche in questo caso comunque si ha un vantaggio: la classe risulta chiaramente firmata come “implements Xlistener”.

10 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Modello 1.1 Container non entra in gioco

11 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti multiListenerDemo package listenersdemo; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MultiListener extends JPanel implements ActionListener { JTextArea topTextArea; JTextArea bottomTextArea; JButton button1, button2; JLabel l = null; final static String newline = "\n"; public static void main(String[] args) { createAndShowGUI(); }

12 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti multiListenerDemo private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("MultiListener"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new MultiListener(); frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); }

13 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti multiListenerDemo public MultiListener() { super(new FlowLayout()); l = new JLabel("Cosa sento io:"); add(l); topTextArea = new JTextArea(); topTextArea.setEditable(false); JScrollPane topScrollPane = new JScrollPane(topTextArea); Dimension preferredSize = new Dimension(200, 75); topScrollPane.setPreferredSize(preferredSize); add(topScrollPane); l = new JLabel("Cosa sente la spia:"); add(l); bottomTextArea = new JTextArea(); bottomTextArea.setEditable(false);

14 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti multiListenerDemo JScrollPane bottomScrollPane = new JScrollPane(bottomTextArea); bottomScrollPane.setPreferredSize(preferredSize); add(bottomScrollPane); button1 = new JButton("Fra Martino campanaro"); add(button1); button2 = new JButton("Dormi tu?"); add(button2); button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(new Spia(bottomTextArea)); setPreferredSize(new Dimension(400, 300)); } public void actionPerformed(ActionEvent e) { topTextArea.append(e.getActionCommand() + newline); topTextArea.setCaretPosition(topTextArea.getDocument().getLength()); }

15 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti multiListenerDemo class Spia implements ActionListener { JTextArea myTextArea; public Spia(JTextArea ta) { myTextArea = ta; } public void actionPerformed(ActionEvent e) { myTextArea.append(e.getActionCommand() + MultiListener.newline); myTextArea.setCaretPosition(myTextArea.getDocument().getLength()); }

16 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Design considerations The most important rule to keep in mind about event listeners that they should execute very quickly. Because all drawing and event-listening methods are executed in the same thread, a slow event-listener method can make the program seem unresponsive and slow to repaint itself. You might choose to implement separate classes for different kinds of event listeners. This can be an easy architecture to maintain, but many classes can also mean reduced performance. When designing your program, you might want to implement your event listeners in a class that is not public, but somewhere more hidden. A private implementation is a more secure implementation.

17 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Low-Level Events and Semantic Events Events can be divided into two groups: low-level events and semantic events. Low-level events represent window-system occurrences or low-level input. Everything else is a semantic event. Examples of low-level events include mouse and key events — both of which result directly from user input. Examples of semantic events include action and item events. Whenever possible, you should listen for semantic events rather than low-level events. That way, you can make your code as robust and portable as possible. For example, listening for action events on buttons, rather than mouse events, means that the button will react appropriately when the user tries to activate the button using a keyboard alternative or a look-and- feel-specific gesture.

18 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Listeners/Adapters public class MyClass implements MouseListener {... someObject.addMouseListener(this);... /* Empty method definition. */ public void mousePressed(MouseEvent e) { } /* Empty method definition. */ public void mouseReleased(MouseEvent e) { } /* Empty method definition. */ public void mouseEntered(MouseEvent e) { } /* Empty method definition. */ public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) {...//Event listener implementation goes here... } }

19 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Listeners/Adapters /* * An example of extending an adapter class instead of * directly implementing a listener interface. */ public class MyClass extends MouseAdapter {... someObject.addMouseListener(this);... public void mouseClicked(MouseEvent e) {...//Event listener implementation goes here... } }

20 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Inner classes //An example of using an inner class. public class MyClass extends JFrame {... someObject.addMouseListener( new MyAdapter());... class MyAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e){...//Event listener implementation goes here... } }

21 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Anonymous Inner classes //An example of using an inner class. public class MyClass extends JFrame {... someObject.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e){...//Event listener implementation goes here... } } });... }

22 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Inner classes An instance of InnerClass can exist only within an instance of EnclosingClass and it has direct access to the instance variables and methods of its enclosing instance.

23 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Listeners supported by all Swing components component listener Listens for changes in the component's size, position, or visibility. focus listener Listens for whether the component gained or lost the ability to receive keyboard input. key listener Listens for key presses; key events are fired only by the component that has the current keyboard focus. mouse listener Listens for mouse clicks and mouse movement into or out of the component's drawing area. mouse-motion listener Listens for changes in the cursor's position over the component. mouse-wheel listener mouse-wheel listener (introduced in 1.4) Listens for mouse wheel movement over the component.

24 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Altri listeners action caret change document undoable edit item list selection window + Listeners speciali per componenti specifiche (treeNodeExpansion, ecc)