CSC 205 Programming II Lecture 5 AWT - I.

Slides:



Advertisements
Similar presentations
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Advertisements

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.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
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.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
CHAPTER-1 SWING JAVA-GUI. HERE ARE SOME TERMS THAT YOU’LL ENCOUNTER IN YOUR LESSON ON GRAPHICS: AWT Swing Applet/JApplet Graphics object init() GUI.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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,
Java Programming 1 Java Programming II Events, AWT, and 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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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,
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.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
Events and Event Handling
Welcome To java
A First Look at GUI Applications Radio Buttons and Check Boxes
Aum Amriteshwaryai Namah
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Responding to Events Event Handling in Java
A First Look at GUI Applications
Lecture 09 Applets.
The Abstract Window Toolkit
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
GUI Programming III: Events
The Abstract Window Toolkit
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
The Abstract Window Toolkit
The Abstract Window Toolkit
Introduction to Computing Using Java
The Abstract Window Toolkit
The Abstract Window Toolkit
The Abstract Window Toolkit
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
GUI building with the AWT
Presentation transcript:

CSC 205 Programming II Lecture 5 AWT - I

Overview Java’s Abstract Window Toolkit provides classes and other tools for building programs that have a graphical user interface. The term “Abstract” refers to the AWT’s ability to run on multiple platforms. Building a GUI involves creating “abstract” components such as buttons and windows, which are then mapped to “concrete” components for a specific platform.

Creating a GUI GUI programming in Java is based on three concepts: Components. A component is an object that the user can see on the screen and interact with. Containers. A container is a component that can hold other components. Events. An event is an action triggered by the user, such as a key press or mouse click.

A GUI Window

Hierarchy of Component Classes The component classes are related to each other through inheritance:

Component Properties Some of the properties shared by all components: Position: The position of the component on the screen. Size: The height and width of the component. Visibility: Whether or not the component is currently visible. Colors: The component’s foreground and background colors.

Creating a Component Object Components are objects, so they’re created by invoking a constructor. A button would be created by using a constructor belonging to the Button class. The most commonly used constructor has one argument (the button’s label): Button b = new Button("Testing"); For a component to be visible, it must be added to a container (typically a frame) by the add method.

Event Listener To detect when an event occurs, a special “listener” object can be attached to a component. When the user performs an action that involves the component, a method belonging to the listener object will be called automatically.

Frames In Java terminology, a frame is a window with a title and a border. A frame may also have a menu bar. Frames play an important role in the AWT because a GUI program normally displays a frame when it’s executed. The DrawableFrame objects used in previous chapters are examples of frames.

Creating a Frame The FrameTest program creates a Frame object and displays it on the screen. This program illustrates three key steps: 1. Using the Frame constructor to create a frame. 2. Setting the size of the frame. 3. Displaying the frame on the screen.

Sample Code // Displays a frame on the screen. import java.awt.*; public class FrameTest { public static void main(String[] args) { Frame f = new Frame("Frame Test"); f.setSize(150, 100); f.setVisible(true); } // WARNING: Frame cannot be closed.

Adding Components to a Frame To add a component to a frame (or any kind of container), the add method is used. add belongs to the Container class, so it’s inherited by Frame and the other container classes. An example of adding a button to a frame: Button b = new Button("Testing"); add(b); These statements would normally go in the constructor for the frame class.

Sample Code import java.awt.*; // Driver class public class ButtonTest { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Testing"); add(b);

Event Listeners Steps involved in handling an event: 1. The user performs an action, causing an event to be triggered (or fired). 2. An object is created that contains information about the event, including an indication of which component was involved. 3. A method that belongs to a listener object is called. The object created in step 2 is passed to the method.

Events When an event occurs, an object is created that contains information about the event. This object will belong to one of several different classes, depending on the nature of the event. These classes all belong to the java.awt.event package. Java divides events into two groups: “high-level” events and “low-level” events.

Events Class Name Description of Event ActionEvent A significant action has been performed on a component (a button was pressed, a list item was double-clicked, or the Enter key was pressed in a text field). AdjustmentEvent The state of an adjustable component (such as a scrollbar) has changed. ItemEvent An item has been selected (or deselected) within a checkbox, choice menu, or list. TextEvent The contents of a text area or text field have changed.

Interfaces Event-handling requires the use of interfaces. An interface looks like a class, except that its methods aren’t fully defined. Each method in an interface has a name, a parameter list, and a result type, but no body. One common interface is named ActionListener: public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent evt); } This resembles a class declaration, except that the word class has been replaced by interface, and the actionPerformed method has no body.

Interfaces The keyword implements is used to tell the compiler that a class will implement a particular interface. A class that implements the ActionListener interface: class class-name implements ActionListener { public void actionPerformed(ActionEvent evt) { … } … // Variables, constructors, and methods, // if desired The class may contain any number of variables, constructors, and methods.

Creating Event Listeners To handle an event, it’s necessary to create an event listener object. This object will be “registered” with a component; when an event occurs that involves the component, one of the listener’s methods will be called. An event listener will be an instance of a “listener class” defined by the programmer.

Creating Event Listeners A listener class must implement one of the interfaces that belong to the java.awt.event package. Listener interfaces for high-level events: Interface Name Required Method ActionListener actionPerformed(ActionEvent evt) AdjustmentListener adjustmentValueChanged(AdjustmentEvent evt) ItemListener itemStateChanged(ItemEvent evt) TextListener textValueChanged(TextEvent evt) Each interface contains a single method. The access modifier for each method is public, and the result type is void.

Creating Event Listeners There’s a similar set of listener interfaces for low-level events. The listener interface for WindowEvent is named WindowListener.

Creating Event Listeners Pressing a button is an action event, so the listener class for a button would need to implement the ActionListener interface. To implement this interface, the class must define a public void method named actionPerformed with a parameter of type ActionEvent. An example of a listener for an action event: class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { … }

Creating Event Listeners After writing a listener class, the next step is to create an instance of the class and connect it to a particular component. In the simplest case, a single listener object will be attached to a single component. Suppose that b is a Button object: Button b = new Button("Change Color"); A listener object can be created by using the constructor for the listener class: ButtonListener listener = new ButtonListener();

Creating Event Listeners listener can now be registered as an action listener for the button: b.addActionListener(listener); It’s sometimes possible to save a statement by combining the creation of the listener object with the call of addActionListener: b.addActionListener(new ButtonListener());

Creating Event Listeners Calling addActionListener creates a link between the Button object and its listener: When the user presses the button, the ButtonListener object’s actionPerformed method will be called.

// Displays a frame containing a single "Close window" // button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest2 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); }

// Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window"); add(b); b.addActionListener(new ButtonListener()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0);