CS 251 Intermediate Programming GUIs: Event Listeners

Similar documents
Java GUI Design: the Basics

CS 251 Intermediate Programming GUIs: Components and Layout

Event Driven Programming

Final Exam CS 251, Intermediate Programming December 13, 2017

PIC 20A GUI with swing

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Graphical User Interfaces 2

COMPSCI 230. Software Design and Construction. Swing

Lecture 5: Java Graphics

GUI Event Handlers (Part I)

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical User Interfaces 2

MIT AITI Swing Event Model Lecture 17

CS108, Stanford Handout #22. Thread 3 GUI

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Graphical User Interfaces 2

CSE 331 Software Design & Implementation

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Lecture 3: Java Graphics & Events


Lecture 19 GUI Events

Graphical User Interface (GUI)

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Graphical User Interface (GUI)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Class 16: The Swing Event Model

Window Interfaces Using Swing Objects

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Window Interfaces Using Swing. Chapter 12

GUI Software Architecture

AP CS Unit 11: Graphics and Events

Java: Graphical User Interfaces (GUI)

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Part I: Learn Common Graphics Components

Java for Interfaces and Networks (DT3010, HT10)

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Window Interfaces Using Swing Objects

Example: Building a Java GUI

AP Computer Science Unit 13. Still More Graphics and Animation.

Example: Building a Java GUI

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

SD Module-1 Advanced JAVA

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

SD Module-1 Advanced JAVA. Assignment No. 4

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

The AWT Event Model 9

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Dr. Hikmat A. M. AbdelJaber

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Jonathan Aldrich Charlie Garrod

Summary. Section 14.1 Introduction. Section 14.2 Java s New Nimbus Look-and-Feel. 618 Chapter 14 GUI Components: Part 1

Chapter 6: Graphical User Interfaces

Swing: Building GUIs in Java

Programming Languages 2nd edition Tucker and Noonan"

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java IDE Programming-I

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Graphical User Interfaces (GUIs)

Introduction to concurrency and GUIs

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Agenda. Container and Component

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

Swing: Building GUIs in Java

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

CS2110. GUIS: Listening to Events

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

Charlie Garrod Michael Hilton

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Introduction to the JAVA UI classes Advanced HCI IAT351

Solution register itself

Graphical interfaces & event-driven programming

Unit 7: Event driven programming

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Programming graphics

Systems Programming Graphical User Interfaces

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Advanced Java Programming (17625) Event Handling. 20 Marks

Introduction. Introduction

CS11 Java. Fall Lecture 4

GUI Forms and Events, Part II

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

Transcription:

CS 251 Intermediate Programming GUIs: Event Listeners Brooke Chenoweth University of New Mexico Fall 2017

What is an Event Listener? A small class that implements a particular listener interface. Listener object can register as a listener for events from event source object. One listener object can listen for events from multiple sources. Multiple listeners can listen for events from the same source.

ActionListener on a Button Implement ActionListener interface public class MyActionListener implements ActionListener { public void actionperformed ( ActionEvent ev) { System. out. println (" Action Happened!"); and register the listener with a component. MyActionListener listener = new MyActionListener (); JButton button = new JButton (" My Button "); button. addactionlistener ( listener );

Anonymous ActionListener For one-off listeners, we often use an anonymous class. JButton button = new JButton (" My Button "); button. addactionlistener ( new ActionListener () { public void actionperformed ( ActionEvent ev) { System. out. println (" Action Happened!"); );

Common component listeners component listener changes in the component s size, position, or visibility. focus listener whether the component gained or lost the keyboard focus. key listener key presses mouse listener mouse clicks, mouse presses, mouse releases and mouse movement into or out of the component s drawing area. mouse-motion listener changes in the mouse cursor s position over the component. mouse-wheel listener mouse wheel movement over the component.

Event Listeners Look at component to see what sort of listeners it can take. Implement listener interface and add to component. Listener interfaces with multiple methods generally have an adapter class that provides empty implementation.

Action Listeners Most common event handler to implement. Action events happen when user performs an action Click a button Select a menu item Press Enter in a text field etc. actionperformed message sent to all action listeners registered with the relevant component.

Click Counter public class ClickCounter extends JFrame implements ActionListener { private JTextField text = new JTextField (20); private JButton button = new JButton (" Click me!"); private int numclicks = 0; public ClickCounter () { super (" Click Counter Demo "); setdefaultcloseoperation ( JFrame. EXIT_ON_CLOSE ); button. addactionlistener ( this ); getcontentpane (). add ( button, BorderLayout. CENTER ); getcontentpane (). add ( text, BorderLayout. PAGE_START ); pack (); public void actionperformed ( ActionEvent e) { numclicks ++; text. settext (" Button Clicked " + numclicks + " times ");

Key Listener Key events indicate when the user is typing. Events fired by component with keyboard focus keytyped User typed a Unicode character keypressed User pressed a key keyreleased User releases a key KeyEvent has methods to tell which key and which modifiers (holding down Ctrl or Shift, for example)

Key Listener Example public class KeyDemo extends JFrame { public KeyDemo () { super (" Key Listener Demo "); setdefaultcloseoperation ( JFrame. EXIT_ON_CLOSE ); JTextArea area = new JTextArea (10, 20); area. addkeylistener ( new KeyAdapter () { public void keypressed ( KeyEvent ev) { System. out. println (" Pressed : " + KeyEvent. getkeytext ( ev. getkeycode ())); ); getcontentpane (). add ( area ); pack (); public static void main ( String [] args ) { new KeyDemo (). setvisible ( true );

Listening to the Mouse There are three kinds of listeners for the mouse. MouseListener clicked, pressed, released, entered, exited MouseMotionListener dragged, moved MouseWheelListener mouse wheel moved The MouseAdapter class implements them all.

Mouse Listener Example public class MouseDemo extends JFrame { public MouseDemo () { super (" Mouse Listener Demo "); setdefaultcloseoperation ( JFrame. EXIT_ON_CLOSE ); JPanel panel = new JPanel (); panel. setpreferredsize ( new Dimension (300,300)); panel. addmouselistener ( new MouseAdapter () { public void mouseclicked ( MouseEvent ev) { System. out. println (" Click " + ev. getbutton () + " at: " + ev. getpoint ()); ); getcontentpane (). add ( panel ); pack (); public static void main ( String [] args ) { new MouseDemo (). setvisible ( true );

Initial thread: main Threads in GUI Event dispatch thread: handles GUI Worker threads: larger tasks in the background. (Use javax.swing.swingworker for these.)

Start GUI on event dispatch thread Previous examples have been living dangerously, creating GUI on main thread. public static void main ( String [] args ) { createandshowgui (); Really should use be creating GUI on event dispatch thread. public static void main ( String [] args ) { SwingUtilities. invokelater ( new Runnable () { public void run () { createandshowgui (); );