1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
For IST410 Students only Events-1 Event Handling.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Event-Driven Programming
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Graphical User Interfaces
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Io package as Java’s basic I/O system continue’d.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
Java Programming 1 Java Programming II Events, AWT, and Swing.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
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.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
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.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
IB103 Week 9 Free Standing Programs Chapter 18. Applets vs. Applications Applications are free standing See Program “Greeting” which prints out Hello.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
A First Look at GUI Applications
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
Event-driven programming for GUI
Introduction to Computing Using Java
Programming Graphical User Interface (GUI)
GUI building with the AWT
Presentation transcript:

1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers  Adding Components to Windows.  Preview: Graphical User Interfaces (GUI) with AWT and Swing.

2 lecture 12Lecture 13 Handling Window Events  In Lecture 11 we considered simple examples of handling keyboard and mouse events. We will now consider handling window events.  As in the examples in Lecture 11, we need to implement the WindowListener interface in order to handle window events.  The WindowListener interface defines seven methods (see the Java API documentation in your JavaCD for details of these methods etc): void windowActivated(WindowEvent we); void windowClosed(WindowEvent we); void windowClosing(WindowEvent we); void windowDeactivated(WindowEvent we); void windowDeiconified(WindowEvent we); void windowIconified(WindowEvent we); void windowOpened(WindowEvent we);  In the following example, we are only interested in responding to the windowclosing event so that we ensure that the window closes on a windowclosing event.

3 lecture 12Lecture 13 Example 1: Handling Window Events import java.awt.*;import java.awt.event.*; public class ClosingMyWindow extends Frame implements WindowListener{ public ClosingMyWindow() { super("An Empty Frame You Can Close!"); setSize(400,400); addWindowListener(this); show(); } public void windowClosing(WindowEvent we) { System.exit(0); } public void windowClosed(WindowEvent we) {} public void windowIconified(WindowEvent we) {} public void windowDeiconified(WindowEvent we) {} public void windowActivated(WindowEvent we) {} public void windowDeactivated(WindowEvent we) {} public void windowOpened(WindowEvent we) {} public static void main(String args [] ) { new ClosingMyWindow(); }}

4 lecture 12Lecture 13 Example 2: Using Event Adapters  The above program has an obvious disadvantage because only the windowClosing() handler has a meaningful body and all the other remaining six methods are stubs.  As we have discussed in Lecture 9, this is an ideal place to make use of event adapters to make our programming easier.  Here is an equivalent program using the WindowAdapte r class: import java.awt.*;import java.awt.event.*; class MyWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent we) { System.exit(0); }} class ClosingWindowUsingAdapter extends Frame{ public ClosingWindowUsingAdapter() { super("An Empty Frame You Can Close!"); setSize(400,400); addWindowListener(new MyWindowListener()); show(); } public static void main(String args [] ) { new ClosingWindowUsingAdapter(); }

5 lecture 12Lecture 13 Example 3: Window Closing with Inner Classes  The following program is yet another version of the window program. The essence of this version is to demonstrate the use of anonymous inner classes. import java.awt.*; import java.awt.event.*; public class AnonymousInnerClass extends Frame { public AnonymousInnerClass() { super("An Empty Frame You Can Close!"); setSize(400,400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); show(); } public static void main(String args [] ) { new AnonymousInnerClass(); }

6 lecture 12Lecture 13 Introduction to Components and Containers  Before considering the next examples, we briefly introduce components and containers.  A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface.  A container is a special component that can hold other components. Examples of Containers are applets, panels and frames.  The functionality of most GUI components is derived from the Component and Container classes in the inheritance hierarchy below.  Exercise: Study the methods of the following classes in the Java 2 SDK documentation. java.lang.Object | java.awt.Component | java.awt.Container | java.awt.Window | java.awt.Frame java.lang.Object | java.awt.Component | java.awt.Container | java.awt.Panel | java.awt.Applet

7 lecture 12Lecture 13 Adding Components to Windows  In the next examples, we introduce other graphics components and add them to our Frame windows. To make the example simple, we will only add three types of components: buttons, text fields and labels  A Button generates action events when pushed. There are many different kinds of button (see the Java 2 SDK documentation for details).  For a class to respond to an action event, it must implement the single method in the ActionListener interface.  A TextField is a single-line area in which text can be entered by the user from the keyboard or text can simply be displayed.  When the user types data into a TextField and presses the Enter key, an action event occurs.  A Label is used to provide text instructions or information on a GUI. Labels are defined in the classes Label and JLabel.

8 lecture 12Lecture 13 Example 4: Counting Number of Button Clicks import java.awt.*; import java.awt.event.*; public class CountButtonPushes extends Frame implements ActionListener { Button button = new Button("Press me"); Label total = new Label( "Running total:"); TextField tally = new TextField(10); int sum = 0; public CountButtonPushes() { super("A Container With Components"); setSize(500,500); setLayout(new FlowLayout()); add ( total ); add ( tally ); add ( button ); button.addActionListener(this); show(); } public void actionPerformed( ActionEvent e ) { sum = sum + 1; // add number to sum tally.setText(Integer.toString(sum)); } public static void main(String args [] ) { new CountButtonPushes(); }

9 lecture 12Lecture 13 Example 4 (cont.)  In applications with Frames and Applets, components are attached to the content pane, which is a Container.  Frequently used methods which originate in class Container are add() and setLayout() as used in Example 4.  The add() method is used for adding components to the content pane.  The setLayout() method is used to specify the layout manager that helps a Container to automatically position and size its components. (We will discuss layout managers in a little more detail in coming lecture)  Suppose we wished to add a reset button to this example so that when the reset button is clicked, the counter is reset to zero.  Then we must change actionPerformed() since it needs to react differently depending on which button ( button or reset ) is pushed.  Notice that both button and reset are going to register as listeners to the action event but behave (i.e., implement the ActionListener interface) differently.  The modified program is shown next.

10 lecture 12Lecture 13 Example 5: Enhancing Example 4. import java.awt.*;import java.awt.event.*; public class CountButtonPushesWithReseter extends Frame{ Button button = new Button("Press me"); Button reset = new Button("Reset total"); Label total = new Label( "Running total:"); TextField tally = new TextField(10); int sum = 0; public CountButtonPushesWithReseter() { super("A Container With Components"); setSize(500,500); setLayout(new FlowLayout()); add(total); add(tally); add (button); add (reset); button.addActionListener(new ButtonListener()); reset.addActionListener(new ResetListener()); show(); } class ButtonListener implements ActionListener{ public void actionPerformed ( ActionEvent e ) { sum = sum + 1; // add number to sum tally.setText(Integer.toString(sum)); }

11 lecture 12Lecture 13 Example 5: Enhancing Example 4 (cont.) class ResetListener implements ActionListener { public void actionPerformed( ActionEvent e ) { sum = 0; // Reset Sum to 0 tally.setText(Integer.toString(sum)); } public static void main(String args [] ) { new CountButtonPushesWithReseter(); }