Java Event Handling CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
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.
GUI programming Graphical user interface-based programming.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Layout Managers Arranges and lays out the GUI components on a container.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
For (int i = 1; i
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.
Object Oriented Programming.  Interface  Event Handling.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Modular Event Handling
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Event-driven programming for GUI
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event-driven Programming Code executed at startup to initialize –Code implemented in constructor Additional code executed on demand when user causes events (button press, etc.) –Usually done in actionPerformed method of the application public void actionPerformed(ActionEvent e) { responseField.setText("Welcome to CSIS 3701!"); }

Event-driven Programming Application registers as an “ ActionListener ” –Can handle “action events” created by components public class Main extends JFrame implements ActionListener { Application tells button to notify it when pressed –Adds itself as an ActionListener to the button helloButton.addActionListener(this); Reference to the application itself

Event-driven Programming Application Construct button Have it notify me about events by passing my own address User presses helloButton.addActionListener(this); Button notifies application actionPerformed(ActionEvent e) location 37A4 37A4

Clock Object as Member Variable ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock

Event Objects Java events are objects with properties/methods –Event object passed to application as parameter public void actionPerformed(ActionEvent e) Contain useful information about event –Example: which component caused event public Object getSource() –Can use to execute different code for different events if (e.getSource() == helloButton) {…

Event-driven Programming Application User presses Button notifies application actionPerformed(ActionEvent e) location 7981 ActionEvent object Source : 7981 helloButton : 7981

Event Objects Example: application to move text either left or right based on which button pressed private JButton leftButton; private JButton rightButton; public Main() { leftButton = new JButton("MOVE LEFT"); rightButton = new JButton("MOVE RIGHT"); leftButton.addActionListener(this); rightButton.addActionListener(this); actionPerformed called if either pressed

Event Objects Use getSource to find whether to move text left or right public void actionPerformed(ActionEvent e) { String temp; if (e.getSource() == leftButton) { temp = rightField.getText(); leftField.setText(temp); rightField.setText(""); } if (e.getSource() == rightButton) { temp = leftField.getText(); rightField.setText(temp); leftField.setText(""); } }

Simple Event Handling Awkward if many event components –Would need separate if for each –Difficult to write, maintain 12 if statements

Arrays of Components Can store groups of similar components as array private compType[] comps = new compType[size]; Use loop to construct, add listeners, add to panel for (int i = 0; i < size; i++) { comps[i] = new compType[parameters]; comps[i].addActionListener(this); panel.add(comps[i]); }

Keypad Example public class Keypad1 extends JFrame implements ActionListener { private JTextField keyField; private static final int KEYS = 12; private JButton[] keys = new JButton[KEYS]; private String[] labels = new String[] {"1","2","3","4","5","6","7","8","9","*","0","#"}; Will use this array to add different labels to each button

Keypad Example public Keypad1() { JPanel buttonPanel = new JPanel(new GridLayout(4, 3)); for (int i = 0; i < KEYS; i++) { keys[i] = new JButton(labels[i]); keys[i].addActionListener(this); buttonPanel.add(keys[i]); } Construct button from i th label in array Listen for its events Add it to the panel

Loops for Event Handling Can iterate through array to handle events –Loop through array in actionPerfrormed –Compare each component to source of event public void actionPerformed(ActionEvent e) { for (int i = 0; i < size; i++) if (e.getSource() == comps[i]) { … code to handle event on that object … }

Loops for Event Handling public void actionPerformed(ActionEvent e) { for (int i = 0; i < KEYS; i++) { if (e.getSource() == keys[i]) { keyField.setText(keyField.getText()+labels[i]); } } } Since we know the i th button caused the event, add the corresponding i th label to the output