1 / 26

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

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. 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. Anabnormal condition as defined by the application logic. Example: The balance of a Bank Account falling below zero.

  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. 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. 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. Checked vs Unchecked Exceptions • Checked exceptions represent abnormal conditions that have to be handled in the code. • Some examples areinvalid user input, violating the application rules, accessing non-existing files etc. • These are subclasses of class Exception.

  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. Checked Exceptions Class FishTank{ public void addFish(Fish fish) throws WaterTooHotException WaterTooColdException LevelTooLowException { if (temperature > x ) thrownewWaterTooHotException(); 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. Checked Exceptions • Example: Let us take our BankAccount class. Show class BankAccount Example1_62

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

  17. GUI Component API • Java: GUI component is a class • Methods • Events JButton

  18. Components of an Application GUI GUI GUI Components JFrame JFrame JPanel containers JPanel JLabel JTextField JLabel JTextField JButton A GUI component is a visible object in a GUI A GUI container is an area where components are placed JButton

  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. 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. 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. 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. Java Event Delegation Model Class ButtonHandler implements ActionListener Class ButtonDemo JButton b = new JButton(“press”); b.addActionListener (new ButtonHandler()) public void actionPerformed (ActionEvent e){ // Display “you pressed” } press

  24. ButtonDemo • Example: Let us see the code for ButtonDemo Show class ButtonDemo Example2_62

  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. Test your understanding • You will get a chance to implement some of these concepts in Lab 2 today.

More Related