1 CS1110 Lecture 23, 18 Nov 2010 Listening to GUIs Reading for today: Ch 17.4.1, 17.4.4. [ Optional: Ch. 12.1 & 12.1.1 (interfaces)] For next lecture:

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 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Introduction to Java 2 Programming
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
TCU CoSc Programming with Java Handling Events.
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.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
CS1110 Lec November 2010 Exceptions in Java 1 Reading for today: 10. Next lecture: Ch 9.3 No labs this week, no TA office hours Wed-Fri, see consultant.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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.
MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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,
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
For (int i = 1; i
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
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.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
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.
Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack,
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,
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
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.
Lesson 28: More on the GUI button, frame and actions.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
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.
GUI Programming using Java - Event Handling
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event-driven programming for GUI
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

1 CS1110 Lecture 23, 18 Nov 2010 Listening to GUIs Reading for today: Ch , [ Optional: Ch & (interfaces)] For next lecture: Ch 10 (exceptions) No labs next week. There are labs on last week of classes, but they will be office hours plus an optional exercise on exceptions (covered on final). Final: Friday Dec 10 th, 9-11:30am, Statler Auditorium. Register conflicts on CMS assignment "final exam conflicts" by Monday November 30 th. A7 (try to make the suggested milestones timeline! Also, typo on pg 3, 6 lines from bottom should be Breakout.main(new String[] {"2","3"}); A6 officially due tonight. If a staff member isn't at their office hours, and the missing person immediately! We might be able to locate a sub --- but you have to tell us there's a problem! keep iClickers out

2 enter ticker symbol last quote:goog 11/17/10 10:54:38 AM record quote clear record goog 11/17/10 10:34:38 AM goog 11/17/10 10:44:38 AM goog 11/17/10 10:54:38 AM record buttonBox leftBox rightBox Blue: "invisible" organization elements; purple: fields symbolEntryrecordslastQuoteDisplay symBox quoteBox

3 In class StockQuoteGUI extending JFrame, we could do either (o1) buttonBox.add(new JButton("record quote")); or (o2) create field JButton rb= new JButton("record quote") buttonBox.add(rb); Which, if any, is the best option? A.o1 and o2 are equal, because they both have the same effect B.o1, because there's no reason that we need to store the name (on the tab) of the JButton anywhere C.o2, because we might need to refer to that JButton D.o2 except with rb being a static variable E.o2 except with rb being a local variable

4 Responding to events (in Java) An event is a mouseclick, a mouse movement into or out of a window, a keystroke, etc. To “listen to” (and hence react to) an event: 1. Write a method in some class that will listen to the event. 2. Let Java know that the method is defined in the class 3. Register as a listener an instance-of-the-class-that contains-the-method with the component where the event could happen (e.g. a JButton) The reason for this tripartite structure is that we want to use the ActionListener interface.

5 Interface java.awt.event.ActionListener From the API: The class that is interested in processing an action event [step 2 from previous slide] implements [ActionListener], and the object created with that class is [step 3] registered with a component, using the component's addActionListener method. When the action event occurs, that object's [step 1] actionPerformed method is invoked. For CS1110, think of ActionListener as an abstract class ---except we write "extends implements ActionListener"--- … with abstract procedure actionPerformed(ActionEvent e), …which we must override.

public class StockQuoteGUI extends JFrame implements ActionListener { // step 2 /** Respond to user (giving a ticker symbol) and hitting "return" */ public void actionPerformed(ActionEvent ae) {// step 1 if (ae.getSource == recordButton) { // record the latest stock quote … } /** Constructor: a new StockQuoteGUI */ public StockQuoteGUI() { recordButton.addActionListener(this); // step 3 … } 6

Question: how do we display (update) the newest "last quote"? – We need to overwrite the previous quote, not just add a new one. 7 Solution: keep a JLabel in the desired location, and just change that JLabel's text: lastQuoteDisplay.setText(s + " " + lq + " + dateFormat.format(lqtime)); repaint();

Caveat re: A7 In some sense, the ACM package hides some of this machinery "under the hood". Read the assignment handout carefully when it comes to making the paddle respond to the mouse. 8