Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
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.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Event Handling Events and Listeners Timers and Animation.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
GUI Programming in Java
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Introduction to Swing. Introduction and Background Swing is Sun’s second Java GUI kit Built on AWT, but most programs will directly use only Swing classes.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
(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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
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.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
UID – Event Handling and Listeners Boriana Koleva
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.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Introduction to visual programming C#. Learning Outcomes In this chapter, you will learn about :  Event-Based Programming  The Event Based Model  Application.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Java Swing One of the most important features of Java is its ability to draw graphics.
CS Lecture 04 Mice Lynda Thomas
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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 Handling and Listeners in SWING The practice of event handling.
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handling CS 21a: Introduction to Computing I First Semester,
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
A Quick Java Swing Tutorial
Events and Event Handling
CHAPTER Reacting to the user.
Handling User Events with Swing
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
GUI Programming III: Events
Event-driven programming for GUI
Introduction to Computing Using Java
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Making Java GUIs Functional
Presentation transcript:

Events in Java Swing Chris North cs3724: HCI

Typical command line program Non-interactive Linear execution program: main() { code; }

Interactive command line program User input commands Non-linear execution Unpredictable order Much idle time program: main() { decl data storage; initialization code; loop { get command; switch(command) { command1: code; command2: code; … }

Typical GUI program GUI program: main() { decl data storage; initialization code; create GUI; register callbacks; main event loop; } Callback1()//button1 press {code; } Callback2() {code; } … User input commands Non-linear execution Unpredictable order Much idle time Event callback procs

GUI Events Window System event loop App1 OK Cancel App2 code: OKbtn_click() { do stuff; } OKbtn_mouseover() { do more stuff; } CancelBtn_click() { do different stuff; } mouse click input device App1 event loop App2 event loop which app? which callback? App2 OK Cancel

Java Swing program Java program: Class{ main() { decl data storage; initialization code; create GUI objects; register listeners; } listener1() {do stuff; } listener2() {do stuff; } … Event loop automatic in separate thread

Example Example: draw program MyDrawClass{ main() { DataStruct drawn_shapes; drawn_shapes.clear(); create Frame, Panel, buttons, … register listeners; } DrawPanel_listener_click() {drawn_shapes.add(new shape); } UndoButton_listener_click() {drawn_shapes.deleteLast(); } …

Java Listeners 1.Register with a component to receive events Give component a ref to your Listener object JButton1.addMouseListener(new myMouseListener) 2.Receive events from component Component will call callback procs on your Listener object myMouseListener.mouseClicked(event) JButton1 myMouse- Listener click 2. mouseClicked( ) 1. addMouseListener( )

Listener API Listeners must inherit from Java Listener base classes ActionListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, … Abstract base classes: xxxxListener Stubbed base classes: xxxxAdapter MouseListener: mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), mouseReleased()

Code button1 = new JButton(“press me”); myListener = new myListenClass; button1.addMouseListener(myListener); // extending a class (“subclassing”): class myListenClass extends MouseAdapter { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } // OR “implementing an interface”: class myListenClass implements MouseListener { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } … } An abstract base class (methods, no code)

Event objects mouseClicked(MouseEvent e) MouseEvent: getX( ), getY( ), getClickCount( ), getSource( ), … For each listener type: Component.addxxxxListener( ) xxxxListener abstract base class xxxxAdapter stubbed base class xxxxEvent

Inheritance with Swing class myPanel extends JPanel { public myPanel(){//constructor // create buttons, … } public void paintComponent(Graphics g){ super.paint(g); //call overriden method // paint stuff here } myPanel creates JPanel via inheritance Override JPanel methods to add functionality

Simplifying: Implements class myPanel extends JPanel implements MouseListener { public myPanel(){//constructor button1 = new JButton(“press me”); button1.addMouseListener(this); add(button1); } public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } … } abstract base class

Simplifying: Anonymous classes class myPanel extends JPanel { public myPanel(){ button1 = new JButton(“press me”); button1.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } ); add(button1); } Defining and instantiating a class on the fly

In JBuilder Application JFrame, JPanel, JButton Layout managers Event listeners

Homework #3 Painter Applet