DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Size: px
Start display at page:

Download "DEMYSTIFYING PROGRAMMING: CHAPTER FOUR"

Transcription

1 DEMYSTIFYING PROGRAMMING: CHAPTER FOUR Chapter Four: ACTION EVENT MODEL 1 Objectives Additional GUI components 1 JLabel 1 JTextField Inductive Pause Events and Interaction 3 Establish an event Model (ActionListener) 3 Register a listener to a GUI object 4 Handle the event when it occurs 4 New GUI components support user interaction Review Concepts 5 Action Event Model 5 Pattern 6 New methods for JTextField Challenge Java Key Words Exercises 7 Coding Exercises 7 Review Questions 7 Index 9

2 Chapter Four: ACTION EVENT MODEL Objectives Language components that support user interaction are introduced. The action event model is demonstrated. This model includes implementing an ActionListener, registering a listener, and building an event handler. Additional GUI components JLabel The JLabel is a label to assist users in understanding the GUI and what is expected of them. JTextField This GUI component is like the JTextArea except that it can have only one line of information on it. The JTextField is usually used to collect user input. 4.2 Inductive Pause Enough talk, let s take time out to observe and think. Look at the applet on the following page. Demystifying Programming Ch4: ActionEvent Model 1

3 Figure 3: ShowName.java Many parts of this applet are familiar; they follow the same patterns that were used in the previous chapter. What do you notice that is different? Which lines contain new elements? What type of object is txtname? On which lines is it referenced? What does the method header on line 41 indicate in its name? (hint: names should be meaningful) What role or purpose is this method performing? On line 42 what do you think is happening with txtname.gettext( )? gettext is a method of class JTextField. If you know the role commonly played by JTextFields, you can probably guess what is happening on line 42. Focus on line 42. What does String username accomplish? What does username accomplish on line 43? Demystifying Programming Ch4: ActionEvent Model 2

4 4.4 Events and Interaction Solutions become more useful when a user can interact with them. Using the mouse or keyboard a user can supply information or request an action. The program must hear this user action and be programmed to respond in an appropriate manner. Many GUI objects can generate events. Establish an event Model (ActionListener) An event model is usually reflected in the class header. In this chapter we use the ActionListener. The class header for the ShowName class should be: public class ShowName extends JApplet implements ActionListener{ Notice that the ShowName class implements ActionListener. The ActionListener model will assist the programmer in building interactive applets. This event model requires an actionperformed method. This actionperformed method defines the event handler for responding to the enter key being pushed while the prompt is in the JTextField. Carefully explore the following ShowName.java code. It contains many new code items but uses several patterns that were used in the previous chapter. Decide if the line contains new elements. Describe the purpose of each line in your own words. Demystifying Programming Ch4: ActionEvent Model 3

5 Figure 2: ShowName.java Register a listener to a GUI object The applet must be told to listen to one or more of the GUI components. In the code ShowName applet the JTextField txtname has a listener registered for it. Identify the line that contains a new pattern in the sample after instantiation of the object txtname. Which line registers the listener? Write the code here. The method has one parameter (this) which refers to the applet and its attachment to the browser. The applet listens to any object that has a listener registered for it. When an appropriate event is triggered, the applet gathers information about the event. Handle the event when it occurs Although information has been gathered about an event, a method the event handler is required to respond to the event. The event handler for an action event must be called actionperformed. The header must always be Demystifying Programming Ch4: ActionEvent Model 4

6 public void actionperformed(actionevent e) The parameter required for this event is always ActionEvent type although it might be called something other than e. For example, the ActionEvent type object might be called evt or george. The code within the curly braces, the method body will always vary depending upon what needs to be done in response to the event. How does the example ShowName.java respond to an ActionEvent? New GUI components support user interaction JLabel Labels are used to provide instructions for users. The text on the face of the label is usually added during instantiation. This GUI object follows the same pattern as the JTextArea. The example provided above has the following code: JLabel lblname; //declared above init( ) //instantiate label and add to Container in body of init lblname = new JLabel("Please enter a name:");//message con.add(lblname); JTextField JTextFields are used to gather information during execution from the user. It follows the same pattern of declaration and instantiation as the JLabel. JTextField txtname; //declared above init( ) //instantiated and added to Container in body of init txtname = new JTextField(15); //size (15) is not required con.add(txtname); 4.5 Review Concepts Action Event Model 1. import the event library 2. implements ActionListener in class header 3. register listener for GUI object 4. create event handler actionperformed( ) Demystifying Programming Ch4: ActionEvent Model 5

7 Pattern Figure 4: Action Event Pattern The pattern is: 1) import event directory and all its files 2) the class header declares that it implements ActionListener 3) object to be listened to must register a listener in init body 4) an event handler (actionperformed) must be designed to respond to event New methods for JTextField gettext( ): The new method is gettext( ). This method is the opposite of settext( ). As settext writes to the object, gettext reads text from an object. This method must be attached with a dot (.) to an object which has text. String username = txtname.gettext(); Integer.parseInt( ): Another special method may also be useful at times. When a user is asked to type a number in a JTextField, this information can be read using gettext( ). However, this information is text and not a number when it is retrieved from a textfield. If we want to do math with it and if we want to store it in a variable declared for an int (whole number), we ll need to first convert this text into an int. The method uses a wrapper class, Integer and the method parseint: int number = Integer.parseInt( txtusernumber.gettext( ) ); The text typed from the keyboard by the user into the JTextField named txtusernumber is read and parsed (converted) into a whole number. This int is then stored in (assigned to) the newly declared int type variable named number. This method is parseint and is attached to the Integer wrapper class object with a dot (. ) symbol. It is the wrapper class Integer which is parsing the String to int. 4.6 Challenge Is it possible to write to a JLabel or JTextField in the same way that we can to a JTextArea? Prove it to yourself. Demystifying Programming Ch4: ActionEvent Model 6

8 4.7 Java Key Words implements addactionlistener(this) actionperformed( ) ActionEvent 4.8 Exercises 1. What does the following code accomplish? String myname = txtname.gettext( ); 2. Instantiate each of the GUI objects declared in the following code. JLabel lblfirst, lbllast; JTextField txtfirst, txtlast; JButton btnadd; public void init( ){ } 3. Add each of the GUI objects instantiated in the previous exercise to the Container container. This code would be placed in the init block following the instantiation. Coding Exercises 4. Produce code following the Design Plan from exercise two (2) above. Debug and test your product. 5. Create a JApplet that will receive a user s name and display the name followed by the stated objectives of this chapter. Review Questions 6. An action listener requires an event handler called. Demystifying Programming Ch4: ActionEvent Model 7

9 7. What library must be imported to support events?. 8. Which of the following lines of code registers a listener? a. btnpress = new JButton( Press ); b. container.add(btnpress); c. btnpress.addactionlistener(this); d. registerlistener(btnpress); Demystifying Programming Ch4: ActionEvent Model 8

10 Index ActionEvent, 4, 5 ActionListener, 1, 3, 5, 6 actionperformed, 3, 4, 5, 6 assigned, 6 container, 7 event, 1, 3, 4, 5, 6, 7 event handler, 1, 3, 4, 5, 6, 7 event model, 1, 3 events, 3, 7 gettext, 2, 6 GUI, 1, 3, 4, 5, 6, 7 import, 5, 6 instantiate, 5 int, 6 Integer.parseInt, 6 JButton, 7 JLabel, 1, 5, 6 JTextArea, 1, 5, 6 JTextField, 1, 2, 3, 4, 5, 6 methods, 6 new, 2, 3, 4, 5, 6, 7 public, 3, 4, 7 register, 5, 6 String, 2, 6 variable, 6 void, 4, 7 wrapper class, 6 Demystifying Programming Ch4: ActionEvent Model 9

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

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 Objectives 1 6.1 Methods 1 void or return 1 Parameters 1 Invocation 1 Pass by value 1 6.2 GUI 2 JButton 2 6.3 Patterns

More information

Introduction. Introduction

Introduction. Introduction Introduction Many Java application use a graphical user interface or GUI (pronounced gooey ). A GUI is a graphical window or windows that provide interaction with the user. GUI s accept input from: the

More information

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points) Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points) Java Interactive GUI Application for Number Guessing with Colored Hints (based on Module 7 material) 1) Develop a Java application that

More information

SINGLE EVENT HANDLING

SINGLE EVENT HANDLING SINGLE EVENT HANDLING Event handling is the process of responding to asynchronous events as they occur during the program run. An event is an action that occurs externally to your program and to which

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised 1/15/10 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField, JTextArea,

More information

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED)

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED) DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED) Chapter Sixteen: Add and Search an Array... 1 Objectives... 1 16.1 Design and Implementation... 1 An Array-based List can

More information

GUI Forms and Events, Part II

GUI Forms and Events, Part II GUI Forms and Events, Part II Quick Start Compile step once always mkdir labs javac PropertyTax6.java cd labs Execute step mkdir 6 java PropertyTax6 cd 6 cp../5/propertytax5.java PropertyTax6.java Submit

More information

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { } CBOP3203 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 object, deciphers it, and processes the user

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Chapter 5: Enhancing Classes

Chapter 5: Enhancing Classes Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

More information

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published

More information

EVENTS, EVENT SOURCES AND LISTENERS

EVENTS, EVENT SOURCES AND LISTENERS Java Programming EVENT HANDLING Arash Habibi Lashkari Ph.D. Candidate of UTM University Kuala Lumpur, Malaysia All Rights Reserved 2010, www.ahlashkari.com EVENTS, EVENT SOURCES AND LISTENERS Important

More information

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

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

An array is a type of variable that is able to hold more than one piece of information under a single variable name. Arrays An array is a type of variable that is able to hold more than one piece of information under a single variable name. Basically you are sub-dividing a memory box into many numbered slots that can

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal Objectives: To gain experience with the programming of: 1. Graphical user interfaces (GUIs), 2. GUI components, and 3. Event handling (required

More information

Window Interfaces Using Swing. Chapter 12

Window Interfaces Using Swing. Chapter 12 Window Interfaces Using Swing 1 Reminders Project 7 due Nov 17 @ 10:30 pm Project 6 grades released: regrades due by next Friday (11-18-2005) at midnight 2 GUIs - Graphical User Interfaces Windowing systems

More information

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20; First Exam Review, Thursday, February 6, 2014 Note: Do not hand in this lab; but use it for review. Review all materials from notes, slides, examples and labs. Here is an overview of topics with some example

More information

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

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Glossary of Terms 2-4 Step by Step Instructions 4-7 HWApp 8 HWFrame 9 Never trust a computer

More information

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20; First Exam Review, Thursday, February 10, 2011 Review all materials from notes, slides, examples and labs. Here is an overview of topics with some example questions in italics. Note: Do not hand in this

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 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 user. But how do GUIs interact with users? How do applications

More information

Lecture (06) Java Forms

Lecture (06) Java Forms Lecture (06) Java Forms Dr. Ahmed ElShafee 1 Dr. Ahmed ElShafee, Fundamentals of Programming I, Introduction You don t have to output everything to a terminal window in Java. In this lecture, you ll be

More information

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

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Chapter 5 Chapter 5 1 Auto-Boxing and Unboxing and Wrapper Classes Many Java library methods work with class objects only Do not accept primitives Use wrapper classes instead!

More information

Agenda. Container and Component

Agenda. Container and Component Agenda Types of GUI classes/objects Step-by-step guide to create a graphic user interface Step-by-step guide to event-handling PS5 Problem 1 PS5 Problem 2 Container and Component There are two types of

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)

DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED) DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED) Chapter Ten: Classes Revisited, Repetition with while... 1 Objectives... 1 10.1 Design... 1 Repetition process pseudocode...

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

Java IDE Programming-I

Java IDE Programming-I Java IDE Programming-I Graphical User Interface : is an interface that uses pictures and other graphic entities along with text, to interact with user. User can interact with GUI using mouse click/ or

More information

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain undo commands, too. Commands A command is something you want someone else to perform. Instead of directly issuing a command, we can write it down and give it to someone to perform. Written commands can be reused, cataloged,

More information

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet CS 209 Spring, 2006 Lab 9: Applets Instructor: J.G. Neal Objectives: To gain experience with: 1. Programming Java applets and the HTML page within which an applet is embedded. 2. The passing of parameters

More information

Assignment 1. Application Development

Assignment 1. Application Development Application Development Assignment 1 Content Application Development Day 1 Lecture The lecture provides an introduction to programming, the concept of classes and objects in Java and the Eclipse development

More information

We are on the GUI fast track path

We are on the GUI fast track path We are on the GUI fast track path Chapter 13: Exception Handling Skip for now Chapter 14: Abstract Classes and Interfaces Sections 1 9: ActionListener interface Chapter 15: Graphics Skip for now Chapter

More information

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277 CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) 562-5800 x 6277 elsaddik @ site.uottawa.ca abed @ mcrlab.uottawa.ca http://www.site.uottawa.ca/~elsaddik/

More information

CS 251 Intermediate Programming GUIs: Event Listeners

CS 251 Intermediate Programming GUIs: Event Listeners 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

More information

Programming graphics

Programming graphics Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it

More information

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference CSI1102: Introduction to Software Design Chapter 5: Enhancing Classes Learning objectives: Enhancing Classes Understand what the following entails Different object references and aliases Passing objects

More information

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

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */ 25 4 GUI GUI GUI 4.1 4.1.1 MouseTest.java /* 1 */ public class MouseTest extends JApplet implements MouseListener /* 2 */ { int x=50, y=20; addmouselistener(this); /* 3 */ super.paint(g); /* 4 */ g.drawstring("hello

More information

Event Driven Programming

Event Driven Programming Event Driven Programming 1. Objectives... 2 2. Definitions... 2 3. Event-Driven Style of Programming... 2 4. Event Polling Model... 3 5. Java's Event Delegation Model... 5 6. How to Implement an Event

More information

Selection. Chapter 7

Selection. Chapter 7 Selection Chapter 7 Chapter Contents Objectives 7.1 Introductory Example: The Mascot Problem 7.2 Selection: The if Statement Revisited 7.3 Selection: The switch Statement 7.4 Selection: Conditional Expressions

More information

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

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM 2:00 PM Friday 8:30 AM 10:30 AM OR request appointment via e-mail

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Chapter 5 Chapter 5 1 Programming with Methods - Methods Calling Methods A method body may contain an invocation of another method. Methods invoked from method main typically

More information

Day 2: Review, ICS3U0, June 2017

Day 2: Review, ICS3U0, June 2017 Day 2: Review, ICS3U0, June 2017 Bubble in your answers as directed. Use a pencil. Put your name on the scantron. Multiple Choice. Select the best answer. Applets 1. After you click a button, e.getactioncommand()

More information

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common. Chapter 6 Class Action In Chapter 1, we explained that Java uses the word class to refer to A set, collection, group, or configuration containing members regarded as having certain attributes or traits

More information

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn Java & Graphical User Interface II Wang Yang wyang AT njnet.edu.cn Outline Review of GUI (first part) What is Event Basic Elements of Event Programming Secret Weapon - Inner Class Full version of Event

More information

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

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!

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! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

More information

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

CST141 JavaFX Events and Animation Page 1

CST141 JavaFX Events and Animation Page 1 CST141 JavaFX Events and Animation Page 1 1 2 3 4 5 6 7 JavaFX Events and Animation CST141 Event Handling GUI components generate events when users interact with controls Typical events include: Clicking

More information

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

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University Java Swing Recitation 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 8 is out Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm No classes,

More information

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces. Lab 15 Name: Checked: Objectives: Learn about listeners, events, and simple animation for interactive graphical user interfaces. Files: http://www.csc.villanova.edu/~map/1051/chap04/smilingface.java http://www.csc.villanova.edu/~map/1051/chap04/smilingfacepanel.java

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

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

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

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

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs) CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox

More information

MVC: Model View Controller

MVC: Model View Controller MVC: Model View Controller Computer Science and Engineering College of Engineering The Ohio State University Lecture 26 Motivation Basic parts of any application: Data being manipulated A user-interface

More information

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming Ernest Ryu UCLA Mathematics Last edited: December 8, 2017 Introductory example When you write an ActionListener for a GUI, you

More information

CS180 Recitation. More about Objects and Methods

CS180 Recitation. More about Objects and Methods CS180 Recitation More about Objects and Methods Announcements Project3 issues Output did not match sample output. Make sure your code compiles. Otherwise it cannot be graded. Pay close attention to file

More information

CSE 1325 Project Description

CSE 1325 Project Description CSE 1325 Summer 2016 Object-Oriented and Event-driven Programming (Using Java) Instructor: Soumyava Das Graphical User Interface (GUI), Event Listeners and Handlers Project IV Assigned On: 07/28/2016 Due

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions DUKE UNIVERSITY Department of Computer Science CPS 001 Fall 2001 J. Forbes Midterm Solutions PROBLEM 1 : (Quick Ones (4 points)) A. Give an example of a variable declaration. int a; B. Given that you wrote

More information

Contents Chapter 1 Introduction to Programming and the Java Language

Contents Chapter 1 Introduction to Programming and the Java Language Chapter 1 Introduction to Programming and the Java Language 1.1 Basic Computer Concepts 5 1.1.1 Hardware 5 1.1.2 Operating Systems 8 1.1.3 Application Software 9 1.1.4 Computer Networks and the Internet

More information

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons CompSci 125 Lecture 17 GUI: Graphics, Check Boxes, Radio Buttons Announcements GUI Review Review: Inheritance Subclass is a Parent class Includes parent s features May Extend May Modify extends! Parent

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1. GUI Applications The C implementation of Python comes with Tkinter for writing Graphical User Interfaces (GUIs). The GUI toolkit that you get automatically with Jython is Swing, which is included with

More information

CS 106A, Lecture 23 Interactors and GCanvas

CS 106A, Lecture 23 Interactors and GCanvas CS 106A, Lecture 23 Interactors and GCanvas suggested reading: Java Ch. 10.5-10.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5

More information

Graphical User Interface

Graphical User Interface Lecture 10 Graphical User Interface An introduction Sahand Sadjadee sahand.sadjadee@liu.se Programming Fundamentals 725G61 http://www.ida.liu.se/~725g61/ Department of Computer and Information Science

More information

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted CS211 GUI Dynamics Announcements Prelim 2 rooms: A-M are in Olin 155 N-A are in Olin 255 Final exam: final exam 5/17, 9-11:30am final review session (TBA, likely Sun 5/15) Consulting: regular consulting

More information

CP122 CS I. Abstract Interfaces, Graphical User Interfaces, Inheritance

CP122 CS I. Abstract Interfaces, Graphical User Interfaces, Inheritance CP122 CS I Abstract Interfaces, Graphical User Interfaces, Inheritance AirPods now available for order Tech News! Tech News! AirPods now available for order Google reportedly stopping work on its own self-driving

More information

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education Day before tests of Java Final test. IDM institution of Bandarawela Project for department of education import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Doenets extends JApplet

More information

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them. Data Classes and Object-Oriented Programming Data classes can be motivated by the need to create data structures that have grouped together a number of variables of simpler type (ints, Strings, arrays)

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

JRadioButton account_type_radio_button2 = new JRadioButton(Current); ButtonGroup account_type_button_group = new ButtonGroup(); Q)Write a program to design an interface containing fields User ID, Password and Account type, and buttons login, cancel, edit by mixing border layout and flow layout. Add events handling to the button

More information

CS Exam 1 Review Suggestions

CS Exam 1 Review Suggestions CS 235 - Fall 2015 - Exam 1 Review Suggestions p. 1 last modified: 2015-09-30 CS 235 - Exam 1 Review Suggestions You are responsible for material covered in class sessions, lab exercises, and homeworks;

More information

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming Prerequisites Aims DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming Version 1.1, October 2003 You should be familiar with the basic Java, including the use of classes. The luej

More information

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING PROFESSOR GODFREY MUGANDA 1. Learn to Generate Random Numbers The class Random in Java can be used to create objects of the class

More information

Contents Introduction 1

Contents Introduction 1 SELF-STUDY iii Introduction 1 Course Purpose... 1 Course Goals...1 Exercises... 2 Scenario-Based Learning... 3 Multimedia Overview... 3 Assessment... 3 Hardware and Software Requirements... 4 Chapter 1

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

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

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions. Parts of a Contract Syntax - Method signature Method name Parameter list Return type Semantics - Comments Preconditions: requirements placed on the caller Postconditions: what the method modifies and/or

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised March 2, 2017 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField,

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 2 2 Develop the layout of those elements 3 3 Add listeners to the elements 9 4 Implement custom drawing 12 1 The StringArt Program To illustrate

More information

CMP 326 Midterm Fall 2015

CMP 326 Midterm Fall 2015 CMP 326 Midterm Fall 2015 Name: 1) (30 points; 5 points each) Write the output of each piece of code. If the code gives an error, write any output that would happen before the error, and then write ERROR.

More information

CS Fall Homework 5 p. 1. CS Homework 5

CS Fall Homework 5 p. 1. CS Homework 5 CS 235 - Fall 2015 - Homework 5 p. 1 Deadline: CS 235 - Homework 5 Due by 11:59 pm on Wednesday, September 30, 2015. How to submit: Submit your files using ~st10/235submit on nrs-projects, with a homework

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 3 2 Develop the layout of those elements 4 3 Add listeners to the elements 12 4 Implement custom drawing 15 1 The StringArt Program To illustrate

More information

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

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning Content Area: Mathematics Course: Computer Programming Grade Level: 11-12 R14 The Seven Cs of Learning Collaboration Character Communication Citizenship Critical Thinking Creativity Curiosity Unit Titles

More information

Graphical Applications

Graphical Applications Graphical Applications The example programs we've explored thus far have been text-based They are called command-line applications, which interact with the user using simple text prompts Let's examine

More information

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics 1 Collections (from the Java tutorial)* A collection (sometimes called a container) is simply an object that

More information

Visit for more.

Visit  for more. Chapter 3: Getting Started with JAVA IDE Programming Informatics Practices Class XI (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra,

More information

Java GUI Design: the Basics

Java GUI Design: the Basics Java GUI Design: the Basics Daniel Brady July 25, 2014 What is a GUI? A GUI is a graphical user interface, of course. What is a graphical user interface? A graphical user interface is simply a visual way

More information

CS 170 Java Programming 1. Week 9: Learning about Loops

CS 170 Java Programming 1. Week 9: Learning about Loops CS 170 Java Programming 1 Week 9: Learning about Loops What s the Plan? Topic 1: A Little Review ACM GUI Apps, Buttons, Text and Events Topic 2: Learning about Loops Different kinds of loops Using loops

More information

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE

More information