CompSci 230 S Programming Techniques

Slides:



Advertisements
Similar presentations
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Advertisements

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
Modular Event Handling
GUIs and Events Rick Mercer.
Events and Event Handling
Chapter 14 Event-Driven Programming
Web Design & Development Lecture 11
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Processing Timer Events
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
GUI Programming III: Events
Event-driven programming for GUI
Introduction to Event Handling
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Making Java GUIs Functional
Presentation transcript:

CompSci 230 S1 2017 Programming Techniques Basic Event Handling

Agenda Agenda Reading Introduction to Event Handling Event Handling with Inner Classes Event Handling with Anonymous Reading Java how to program Late objects version (D & D) Chapter 12 The Java Tutorial: Introduction to Event Listeners http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html Lecture17

1.Introduction GUIs are event driven. When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform a task. The code that performs a task in response to an event is called an event handler, and the process of responding to events is known as event handling. On-screen components cause events to occur when they are clicked / interacted with Program’s execution is indeterminate Lecture17

1.Introduction Definition Event source: a GUI component that generates / fires an event Event: a user interaction (e.g. a click on the button) Event listener: An object that has implemented event handlers to react to an event Note: the delegation event model - event listeners must be registered with an event source in order to receive notification Listener’s appropriate method will be called when event occurs (e.g. when the button is clicked) Lecture17

How Event Handling Works Each event type has one or more corresponding event- listener interfaces. ActionEvents are handled by ActionListeners MouseEvents are handled by MouseListeners and MouseMotionListeners When the user presses Enter in a JTextField, an ActionEvent occurs. Processed by an object that implements the interface ActionListener . To handle ActionEvents, a class must implement interface ActionListener and declare method actionPerformed. This method specifies the tasks to perform when an ActionEvent occurs. Lecture17

1.Introduction Java Event Hierarchy java.lang.Object +--java.util.EventObject +--java.awt.AWTEvent +--java.awt.event.ActionEvent +--java.awt.event.TextEvent +--java.awt.event.ComponentEvent +--java.awt.event.FocusEvent +--java.awt.event.WindowEvent +--java.awt.event.InputEvent +--java.awt.event.KeyEvent +--java.awt.event.MouseEvent import java.awt.event.*; Event When this type of event occurs ActionEvent the user clicks on a Button, the user presses ‘ENTER' inside a JTextField. MouseEvent the user presses, releases, moves the mouse. KeyEvent the user presses, releases a key on the keyboard Lecture17

2.Event Handling with Inner Classes Before an application can respond to an event for a particular GUI component, you must perform several coding steps: Create a class that represents the event handler. Implement an appropriate interface, known as an event-listener interface, in the class from Step 1. Create an instance of that class (i.e. an event listener) Register the listener with the GUI component(s). This is known as registering the event handler. If you forget to register for a particular GUI’s component’s event type, events of that type will be ignored. Inner class private class MyHandler implements ActionListener { public void actionPerformed(ActionEvent e) { ... } MyHandler handler = new MyHandler(); Must register for each text field. textField1.addActionListener( handler ); textField2.addActionListener( handler ); Lecture17

2.Event Handling JButton & JLabel The most common component — a button is a clickable onscreen region that the user interacts with to perform a single command JLabel: A text label is simply a string of text displayed on screen in a graphical program. Labels often give information or describe other components Methods: public JButton(String text) OR public JLabel(String text) Creates a new button / label with the given string as its text. public String getText() Returns the text showing on the button / label. public void setText(String text) Sets button / label's text to be the given string. Lecture17

2.Event Handling JTextField & JTextArea A text field is like a label, except that the text in it can be edited and modified by the user. Text fields are commonly used for user input, where the user types information in the field and the program reads it JTextArea: A text area is a multi-line text field Methods: public JTextField(int columns) public JTextArea(int lines, int columns) Creates a new text field that is the given number of columns (letters) wide. public String getText() Returns the text currently in the field. public void setText(String text) Sets field's text to be the given string. Lecture17

2.Event Handling Example: SimpleDemo Whenever the user presses 'Enter' in either of the first two JTextFields the third JTextField displays a String combining the shorter of the two words from the first JTextFields followed by the longer. public class SimpleDemo extends JFrame { private JTextField textField1, textField2, textField; public SimpleDemo() { ... MyHandler handler = new MyHandler(); textField1.addActionListener( handler ); textField2.addActionListener( handler ); } private class MyHandler implements ActionListener { @Override public void actionPerformed( ActionEvent e ) { String word1 = textField1.getText(); textField.setText(result); Lecture17

Exercise 1 The user enters integers into the first two TextFields, presses the '+' JButton and the result is displayed in the third JTextField. public class SimpleCalculator extends Jframe { ... addButton.addActionListener(______); private class MyHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int num1 = Integer.parseInt(textField1.getText()); int num2 = Integer.parseInt(textField2.getText()); } Lecture17

3.Event Handling with Anonymous If you have a number of buttons that do almost the same thing. You can create one listener for all the keys. However, there is no need to define a named class simply to add a listener object to a button. Note: The compiler creates names for anonymous classes. (the enclosing class name followed by $ followed by a number, e.g., TextFieldDemo$1.class file public class TextFieldDemo extends JFrame { private JTextField textField1, textField2, textField; private JButton showButton; public TextFieldDemo() { ... textField1.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { showButton.setText(textField1.getText().toUpperCase()); } }); Lecture17

3.Event Handling with Anonymous Example Case 1: the user presses 'Enter' in the first JTextField, the JButton displays a the corresponding text in upper case. Case 2: the user presses ‘Enter’ in the second JTextField, the JButton displays “hello, “ and followed by the corresponding text. Two different event handling procedures inside the actionPerformed method => If we use a local class, then we will need to use the getSource() to return a reference to the event source (textfield1 or textfield2), or We use two anonymous classes to handle the event separately. Lecture17

3.Event Handling with Anonymous Case 1 Convert to uppercase public class TextFieldDemo extends JFrame { private JTextField textField1, textField2, textField; private JButton showButton; public TextFieldDemo() { ... textField1.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { showButton.setText(textField1.getText().toUpperCase()); } }); Lecture17

3.Event Handling with Anonymous Case 2 “hello, “ and followed by the corresponding text public class TextFieldDemo extends JFrame { private JTextField textField1, textField2, textField; private JButton showButton; public TextFieldDemo() { ... textField1.addActionListener( new ActionListener() { }); textField2.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { showButton.setText("hello, " + textField2.getText()); } Lecture17

Exercise 2 This JPanel contains a JButton and a JTextField which initially is empty. The first time the user presses the 'PRESS ME' JButton the JTextField displays "YOU RANG!", the second time the user presses the 'PRESS ME' JButton the JTextField displays "NOT NOW!" and the third time "MAYBE LATER!" is displayed, then "YOU RANG!" again and so on. Some JPanel screenshots: Lecture17