ACM/JETT Workshop - August 4-5, 2005 1 06.ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
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 
Java Programming Chapter 10 Graphical User Interfaces.
Welcome to CIS 083 ! Events CIS 068.
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,
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.
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,
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.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– 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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
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.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
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.
1 Lecture 8: User Interface Components with Swing.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
GUI Programming using Java - Event Handling
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical User Interfaces in Java Event-driven programming
Events, Event Handlers, and Threads
Chapter 15 Event-Driven Programming and Animations Part 1
Presentation transcript:

ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing

ACM/JETT Workshop - August 4-5, Topics What are Exceptions and how to handle them? What are Inner classes? How do we build a Graphical User Interface (GUI) to an application using Swing classes What is Event Delegation Model

ACM/JETT Workshop - August 4-5, Exceptions An exception is An error condition that any program may encounter. Example: a division by zero, or accessing an index that is out of bounds of an array. An abnormal condition as defined by the application logic. Example: The balance of a Bank Account falling below zero.

ACM/JETT Workshop - August 4-5, Exceptions An exception handler is the method or code that handles the exception. Handling an exception may involve taking a corrective action. Raising an exception is –Interrupting the program at the point where exception occurs and calling the exception handler. Propagating an exception is –Passing the responsibility of handling the exception to the calling code.

ACM/JETT Workshop - August 4-5, Exceptions A typical scenario: If an abnormal condition occurs, –an exception is raised; –the program execution is interrupted and –The control is transferred to the exception- handler (if there is any) –If the exception happens and there is no handler, the program terminates.

ACM/JETT Workshop - August 4-5, Exceptions Exceptions in Java are defined as classes. In Java you can handle exceptions that are already defined. You can define your own exceptions, where an exception is a subclass of java.lang.Exception. In Java all exceptions are instances of java.lang.Exception or one of its subclasses.

ACM/JETT Workshop - August 4-5, Checked vs Unchecked Exceptions Checked exceptions represent abnormal conditions that have to be handled in the code. Some examples are invalid user input, violating the application rules, accessing non-existing files etc. These are subclasses of class Exception.

ACM/JETT Workshop - August 4-5, Checked Exceptions Assume we have a class called FishTank with a method to addFish(). The addFish() method may throw a number of Exceptions with a throws clause. The throws clause of addFish() method indicates to client programmers what exceptions they may have to handle when they invoke this method.

ACM/JETT Workshop - August 4-5, Checked Exceptions Class FishTank { public void addFish (Fish fish) throws WaterTooHotException WaterTooColdException LevelTooLowException { if (temperature > x ) throw new WaterTooHotException(); else if (..) … } The throws clause of addFish() method indicates to client programmers what exceptions they may have to handle when they invoke this method.

ACM/JETT Workshop - August 4-5, Checked Exceptions Example: Let us take our BankAccount class. Show class BankAccount Example1_62

ACM/JETT Workshop - August 4-5, Unchecked Exceptions Unchecked exceptions represent program defects like accessing a null pointer or accessing an index that is out of bounds for an array.

ACM/JETT Workshop - August 4-5, Inner Classes An inner class is a class defined inside another class. Similar to nested classes in C++, but more flexible & more powerful. Inner classes are useful because: An object of an inner class can access private fields and methods of outer class. Can be hidden from other classes in the same package. Good for information hiding. Anonymous inner classes (inner classes with no name) are convenient to handle events in event- driven programs.

ACM/JETT Workshop - August 4-5, Inner Classes Types of inner classes Local inner classes Anonymous Inner classes Static inner classes Note: We may not have time to discuss the details of inner classes in this lecture series. But we will see an example of using one in the ButtonDemo class at the end of this lecture.

ACM/JETT Workshop - August 4-5, Creating a Graphic User Interface (GUI) A Graphical user Interface (GUI) enables you to interact with an application by directly manipulating GUI widgets like windows, buttons, menus etc. A GUI handles three functions: –input, –output, and –data handling.

ACM/JETT Workshop - August 4-5, Creating a Graphic User Interface (GUI) Let us see how we can design a simple GUI for our banking application; We design the GUI for the Bank, to enable a user to query for the balance in her account.

ACM/JETT Workshop - August 4-5, Example: A GUI for to get balance in an account Press Sunrise Bank Enter your account id Your balance is $100 5 th August, 2005 Label Text Field Button Text Area Window These GUI components are implemented as Java classes in Abstract Window Toolkit (AWT) and Swing libraries.

ACM/JETT Workshop - August 4-5, GUI Component API Java: GUI component is a class Methods Events JButton

ACM/JETT Workshop - August 4-5, Components of an Application GUI JPanel JButton JFrame JLabel GUIGUI Components JFrame JPanel JButton JLabel containers JTextField A GUI component is a visible object in a GUI A GUI container is an area where components are placed

ACM/JETT Workshop - August 4-5, The Event Delegation Model In Java, GUI processing is event-based. Code is executed when events are generated by user actions, such as clicking buttons, mouse movements, keystrokes etc.

ACM/JETT Workshop - August 4-5, Java Event Delegation Model GUI components generate specific events when the user interacts with them. The component that generates an event is called an event source. Information about a GUI event is stored in an object of a class type AWTEvent. when a GUI component is created, an appropriate event listener is registered with it. The event listener handles the component's events. When an event occurs, the GUI system notifies the registered listeners by calling the appropriate event handling method.

ACM/JETT Workshop - August 4-5, Java Event Delegation Model Delegating the responsibility of handling an event to an object that is an appropriate listener is called event delegation model.

ACM/JETT Workshop - August 4-5, Java Event Delegation Model Let us go through a simple scenario, where a ButtonDemo class contains a Button (button1). ButtonDemo has an inner class called ButtonHandler that handles the button press event from button1. In order to register as an event listener, ButtonHandler implements an ActionListener interface and implements the method, actionPerformed(). The method, actionPerformed() is the event-handler. When button1 is pressed, the actionPerformed method of ButtonHandler is invoked. The method displays the message,”You pressed” in a MessageBox.

ACM/JETT Workshop - August 4-5, Java Event Delegation Model JButton b = new JButton(“press”); b.addActionListener (new ButtonHandler()) press Class ButtonDemo Class ButtonHandler implements ActionListener public void actionPerformed (ActionEvent e){ // Display “you pressed” }

ACM/JETT Workshop - August 4-5, ButtonDemo Example: Let us see the code for ButtonDemo Show class ButtonDemo Example2_62

ACM/JETT Workshop - August 4-5, Test your understanding You should now have an idea of what Java Exceptions are and how to handle exceptions when we call methods. Building a Graphical User Interface (GUI) to an application using Swing classes. The important steps in the Event Delegation Model. The concept of Inner classes and how they are used as event handlers.

ACM/JETT Workshop - August 4-5, Test your understanding You will get a chance to implement some of these concepts in Lab 2 today.