Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ACM/JETT Workshop - August 4-5, 2005 1 06.ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing."— Presentation transcript:

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

2 ACM/JETT Workshop - August 4-5, 2005 2 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

3 ACM/JETT Workshop - August 4-5, 2005 3 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.

4 ACM/JETT Workshop - August 4-5, 2005 4 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.

5 ACM/JETT Workshop - August 4-5, 2005 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.

6 ACM/JETT Workshop - August 4-5, 2005 6 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.

7 ACM/JETT Workshop - August 4-5, 2005 7 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.

8 ACM/JETT Workshop - August 4-5, 2005 8 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.

9 ACM/JETT Workshop - August 4-5, 2005 9 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.

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

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

12 ACM/JETT Workshop - August 4-5, 2005 12 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.

13 ACM/JETT Workshop - August 4-5, 2005 13 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.

14 ACM/JETT Workshop - August 4-5, 2005 14 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.

15 ACM/JETT Workshop - August 4-5, 2005 15 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.

16 ACM/JETT Workshop - August 4-5, 2005 16 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.

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

18 ACM/JETT Workshop - August 4-5, 2005 18 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

19 ACM/JETT Workshop - August 4-5, 2005 19 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.

20 ACM/JETT Workshop - August 4-5, 2005 20 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.

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

22 ACM/JETT Workshop - August 4-5, 2005 22 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.

23 ACM/JETT Workshop - August 4-5, 2005 23 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” }

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

25 ACM/JETT Workshop - August 4-5, 2005 25 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.

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


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

Similar presentations


Ads by Google