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

Similar documents
Introduction. Introduction

EVENTS, EVENT SOURCES AND LISTENERS

Agenda. Container and Component

MIT AITI Swing Event Model Lecture 17

Swing/GUI Cheat Sheet

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

AP CS Unit 11: Graphics and Events

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

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

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

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

To gain experience using GUI components and listeners.

Java - Applets. public class Buttons extends Applet implements ActionListener

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

Class 16: The Swing Event Model

Part 3: Graphical User Interface (GUI) & Java Applets

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

Chapter 12 Advanced GUIs and Graphics

Java Programming Lecture 6

Graphical Applications

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

Packages: Putting Classes Together

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

Programming Language Concepts: Lecture 8

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Adding Buttons to StyleOptions.java

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Systems Programming Graphical User Interfaces

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

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

SINGLE EVENT HANDLING

Part I: Learn Common Graphics Components

Window Interfaces Using Swing. Chapter 12

Graphical User Interfaces. Swing. Jose Jesus García Rueda

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

navlakhi.com / navlakhi.education / navlakhi.mobi / navlakhi.org 1

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

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

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

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Swing Programming Example Number 2

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!

Java Event Handling -- 1

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

H212 Introduction to Software Systems Honors

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

Graphical User Interfaces. Comp 152

Example: Building a Java GUI

Example: Building a Java GUI

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

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

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

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

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

Chapter 7: A First Look at GUI Applications

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

Swing from A to Z Some Simple Components. Preface

Advanced Java Unit 6: Review of Graphics and Events

User interfaces and Swing

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CS 106A, Lecture 23 Interactors and GCanvas

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

CoSc Lab # 5 (The Controller)

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

Handling Mouse and Keyboard Events

CSC 1214: Object-Oriented Programming

Graphic User Interfaces. - GUI concepts - Swing - AWT

CS 251 Intermediate Programming GUIs: Components and Layout

RAIK 183H Examination 2 Solution. November 11, 2013

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

COMPSCI 230. Software Design and Construction. Swing

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

CS 251 Intermediate Programming GUIs: Event Listeners

Window Interfaces Using Swing Objects

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Original GUIs. IntroGUI 1

Graphical User Interfaces (GUIs)

CS111: PROGRAMMING LANGUAGE II

Jonathan Aldrich Charlie Garrod

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

CHAPTER 2. Java Overview

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

Solution register itself

Java Applets / Flash

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

Window Interfaces Using Swing Objects

Extra Credit Lab. Your job is to catch any syntax errors a user might make. Two examples follow. In the panel on the left the user has used

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

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Introduction to the JAVA UI classes Advanced HCI IAT351

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

RAIK 183H Examination 2 Solution. November 10, 2014

Transcription:

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 s interaction. The user clicks on the button ActionEvent Some event handler actionperformed(actionevent e) {

Event Delegation Model describes the steps that need to be followed by a programmer in order to insert codes for event handling in a program. With this model, events are sent to the component from which the event originated, but it is up to each component to propagate the event to one or more registered classes called listeners. Listeners contain event handlers that receive and process the event. In this way, the event handler can be in an object separate from the component. Listeners are classes that implement the EventListener interface.

An event can be sent to many event handlers. Event handlers register with components when they are interested in events generated by that component.

Client objects (handlers) register with a GUI component that they want to observe GUI components trigger only the handlers for the type of event that has occurred Most components can trigger more than one type of event. The delegation model distributes the work among multiple classes.

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class btnlistener extends JApplet implements ActionListener { public void init(){ Container conpane=getcontentpane(); conpane.setlayout(new FlowLayout()); JButton btntest=new JButton("Press Me"); btntest.addactionlistener(this); conpane.add(btntest); Make sure the java.awt.event.* package is imported Use the listener interface together with the implements statements public void actionperformed(actionevent e) { JOptionPane.showMessageDialog( null, "Why did you press me?" );

import javax.swing.*; import java.awt.*; public class colorapplet extends JApplet { public void init( ){ Container conpane=getcontentpane( ); conpane.add(new Pattern()); Save this class as colorapplet.java

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Pattern extends JPanel implements ActionListener { private JButton btnred,btngreen,btnblue; public Pattern() { btnred = new JButton("red"); btngreen = new JButton("green"); btnblue = new JButton("blue"); btnred.addactionlistener(this); btngreen.addactionlistener(this); btnblue.addactionlistener(this); add(btnred); add(btngreen); add(btnblue); public void actionperformed(actionevent e) { Object source = e.getsource( ); Color clr = Color.white; if (source==btnred) clr = Color.red; else if (source==btngreen) clr = Color.green; else if (source==btnblue) clr = Color.blue; setbackground(clr); repaint(); Save this class as Pattern.java implements ActionListener When the Button object is clicked on with the mouse, an ActionEvent is sent. The ActionEvent is received through the actionperformed() method of any ActionListener that is registered on the button through its addactionlistener() method

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculateapplet extends JApplet implements ActionListener { JTextField txt1,txt2,txt3; JLabel lbl1,lbl2,lbl3; public void init( ) { Container conpane = getcontentpane( ); conpane.setlayout(new GridLayout(3,2)); lbl1 = new JLabel("First Number"); lbl2 = new JLabel("Second Number"); lbl3 = new JLabel("Sum of two integers"); txt1 = new JTextField("0",10); txt2 = new JTextField("0",10); txt3 = new JTextField("0",10); txt1.addactionlistener(this); txt2.addactionlistener(this); txt3.addactionlistener(this); conpane.add(lbl1); conpane.add(txt1); conpane.add(lbl2); conpane.add(txt2); conpane.add(lbl3); conpane.add(txt3); public void actionperformed(actionevent e) { int num1 = Integer.parseInt(txt1.getText( )); int num2 = Integer.parseInt(txt2.getText( )); int num3 = num1 + num2; txt3.settext(string.valueof(num3));

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Alphabet extends JApplet implements ItemListener { private JTextField txtinput; private JCheckBox chkbold,chkitalic; public void init( ){ Container conpane = getcontentpane( ); conpane.setlayout(new FlowLayout( )); txtinput = new JTextField("",20); chkbold = new JCheckBox("Bold",false); chkitalic = new JCheckBox("Italic",false); chkbold.additemlistener(this); chkitalic.additemlistener(this); conpane.add(txtinput); conpane.add(chkbold); conpane.add(chkitalic); public void itemstatechanged(itemevent e){ int t=0,c=0; Font font; if (chkbold.isselected( )) t = Font.BOLD; if (chkitalic.isselected( )) { c = Font.ITALIC; font = new Font("Serif",t + c, 14); txtinput.setfont(font); There are a few components that use the ItemListener of the event listener. Among the components used are the JCheckBox and JList. The ItemListener performs the itemstatechanged method to implement action to the events. While additemlistener is used to register the component.

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RGBColor extends JApplet implements AdjustmentListener { private JLabel lblred; private JLabel lblgreen; private JLabel lblblue; private JScrollBar barred; private JScrollBar bargreen; private JScrollBar barblue; private JPanel panelcolor; public void init() { Container conpane = getcontentpane(); JPanel p = new JPanel( ); p.setlayout(new GridLayout(3, 2)); lblred = new JLabel("Red 0"); p.add(lblred); barred=new JScrollBar(Adjustable.HORIZONTAL, 0,0, 0, 255); p.add(barred); barred.setblockincrement(16); barred.addadjustmentlistener(this); The java file continues on next page

lblgreen = new JLabel("Green 0"); p.add(lblgreen); bargreen = new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0,255); p.add(bargreen); bargreen.setblockincrement(16); bargreen.addadjustmentlistener(this); lblblue = new JLabel("Blue 0"); p.add(lblblue); barblue= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0,255); p.add(barblue); barblue.setblockincrement(16); barblue.addadjustmentlistener(this); conpane.add(p, "South"); panelcolor = new JPanel(); panelcolor.setbackground(new Color(0, 0, 0)); conpane.add(panelcolor, "Center"); public void adjustmentvaluechanged(adjustmentevent evt){ lblred.settext("red " + barred.getvalue()); lblgreen.settext("green " + bargreen.getvalue()); lblblue.settext("blue " + barblue.getvalue()); panelcolor.setbackground(new Color(barRed.getValue(), bargreen.getvalue( ), barblue.getvalue())); panelcolor.repaint();

Q1. Write a temperature conversion applet that converts from Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a JTextField). A JLabel should be used to display the converted temperature. Use the following formula for the conversion:

Q2. Enhance the temperature conversion applet of Q1 by adding the Kelvin temperature scale. The applet should also allow the user to make conversions between any two scales. Use the following formula for the conversion between Kelvin and Celsius (in addition to the formula in Q1):