GUI III IS 313 5.6.03.

Slides:



Advertisements
Similar presentations
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Advertisements

Event Handling.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
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.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Event Handling in Java CSE Software Engineering Spring 2000 By Prasad.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to Java Programming
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS Lecture 04 Mice Lynda Thomas
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
GUI Programming using Java - Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Object-Oriented Programming (Java), Unit 22
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Inner Classes 29-Nov-18.
Chapter 16 Event-Driven Programming
Java Inner Classes.
Events, Event Handlers, and Threads
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Making Java GUIs Functional
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Final project.
Inner Classes.
Presentation transcript:

GUI III IS 313 5.6.03

Outline Quiz #2 Homework #2 questions? Event handling

Event Handling

Event handling

Menu item example JMenu fileMenu = new JMenu ("File"); JMenuItem exitItem = new JMenuItem ("Exit"); exitItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) System.exit(0); } );

Button example cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) ListDialog.dialog.setVisible(false); } });

Getting a value back button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) String selectedName = ListDialog.showDialog(null, name.getText()); name.setText(selectedName); } });

MouseListener example blankArea.addMouseListener(new MouseAdapter () { public void mouseClicked(MouseEvent e) MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });

Anonymous class new ActionListener () { public void actionPerformed (ActionEvent e) System.exit(0); }

Anonymous class diagram

Mouse example again blankArea.addMouseListener(new MouseAdapter () { public void mouseClicked(MouseEvent e) MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });

Access to External Values Instance variables of enclosing class Final variables in creating method

Access to Values public class MyImagePanel { String m_baz; public void foo () { ... method body ... } ... rest of class ... public void addImageLabel (String caption) { JLabel lbl = new JLabel (caption); final MyImage img = new MyImage (caption); lbl.addMouseListener ( new MouseAdapter () { public void mouseClicked (MouseEvent e) { // need to access the outer class bar(MyImagePanel.this); // need to call outer class method // access final variable // access instance variable MyImagePanel.foo (img, m_baz); }); ...

MouseListener interface void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e)

Event handling strategies Location of EH code external component class named inner class anonymous inner class Base class or interface used Implement XxxListener Extend XxxAdapter

Recommendation Use anonymous inner class Extend Adapter unless EH is large, then used named inner class Extend Adapter unless all interface methods needed Note: no ActionAdapter class

Event handling thread Can only process one event at a time Time-consuming event handlers tie up the interface no other actions registered

Example