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

06.ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing

Embed Size (px)

DESCRIPTION

06.ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

1 ACM/JETT Workshop - August 4-5, 2005

06.ExceptionHandling and User Interfaces

(Event Delegation, Inner classes) using Swing

Page 2: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

2 ACM/JETT Workshop - August 4-5, 2005

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

Page 3: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

3 ACM/JETT Workshop - August 4-5, 2005

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.

Page 4: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

4 ACM/JETT Workshop - August 4-5, 2005

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.

Page 5: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

5 ACM/JETT Workshop - August 4-5, 2005

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.

Page 6: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

6 ACM/JETT Workshop - August 4-5, 2005

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.

Page 7: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

7 ACM/JETT Workshop - August 4-5, 2005

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.

Page 8: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

8 ACM/JETT Workshop - August 4-5, 2005

Checked ExceptionsAssume 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.

Page 9: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

9 ACM/JETT Workshop - August 4-5, 2005

Checked ExceptionsClass FishTank{

public void addFish(Fish fish) throwsWaterTooHotExceptionWaterTooColdExceptionLevelTooLowException

{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.

Page 10: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

10 ACM/JETT Workshop - August 4-5, 2005

Checked Exceptions• Example: Let us take our BankAccount

class.

Show class BankAccountExample1_62

Page 11: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

11 ACM/JETT Workshop - August 4-5, 2005

Unchecked Exceptions• Unchecked exceptions represent program

defects like accessing a null pointer or accessing an index that is out of bounds for an array.

Page 12: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

12 ACM/JETT Workshop - August 4-5, 2005

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.

Page 13: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

13 ACM/JETT Workshop - August 4-5, 2005

Inner ClassesTypes of inner classes• Local inner classes• Anonymous Inner classes• Static inner classesNote: 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.

Page 14: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

14 ACM/JETT Workshop - August 4-5, 2005

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.

Page 15: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

15 ACM/JETT Workshop - August 4-5, 2005

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.

Page 16: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

16 ACM/JETT Workshop - August 4-5, 2005

Example: A GUI for to get balance in an account

PressPress

Sunrise Bank

Enter your account id

Your balance is $100

5th 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.

Page 17: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

17 ACM/JETT Workshop - August 4-5, 2005

GUI Component API

• Java: GUI component is a class

• Methods

• EventsJButton

Page 18: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

18 ACM/JETT Workshop - August 4-5, 2005

Components of an Application GUI

JPanel

JButton

JFrame

JLabel

GUI GUI Components

JFrame

JPanel

JButtonJLabel

containers

JTextField JTextField

A GUI component is a visible object in a GUI A GUI container is an area where components

are placed

Page 19: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

19 ACM/JETT Workshop - August 4-5, 2005

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.

Page 20: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

20 ACM/JETT Workshop - August 4-5, 2005

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.

Page 21: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

21 ACM/JETT Workshop - August 4-5, 2005

Java Event Delegation Model

• Delegating the responsibility of handling an event to an object that is an appropriate listener is called event delegation model.

Page 22: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

22 ACM/JETT Workshop - August 4-5, 2005

Java Event Delegation ModelLet 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.

Page 23: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

23 ACM/JETT Workshop - August 4-5, 2005

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”

}

Page 24: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

24 ACM/JETT Workshop - August 4-5, 2005

ButtonDemo• Example: Let us see the code for

ButtonDemo

Show classButtonDemoExample2_62

Page 25: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

25 ACM/JETT Workshop - August 4-5, 2005

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.

Page 26: 06.ExceptionHandling and User Interfaces  (Event Delegation, Inner classes) using Swing

26 ACM/JETT Workshop - August 4-5, 2005

Test your understanding

• You will get a chance to implement some of these concepts in Lab 2 today.