Aum Amriteshwaryai Namah

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.
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.
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.
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.
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.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
GUI Programming in Java
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
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.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
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.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Appendix I GUI Components and Event Handling
Responding to Events Event Handling in Java
A First Look at GUI Applications
Abstract Window ToolKit (AWT)
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT.
Introduction to Computing Using Java
AWT Components and Containers
Programming Graphical User Interface (GUI)
Advanced GUIs and Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Aum Amriteshwaryai Namah Unit 5 GUI

Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface (GUI) applications. Event Driven Programming Java API for GUI – java.awt and javax.swing.

Java APIs for GUI There are two sets of Java APIs for GUI programming: AWT (Abstract Windowing Toolkit) and Swing. AWT APIs were introduced in JDK 1.0. Swing APIs, a much more comprehensive set of graphics libraries that enhances the AWT counterparts, has been integrated into core Java since JDK 1.2. These provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications.

Sample GUI using AWT

Container vs. Component

Container vs. Component Two groups of classes are involved in a GUI program: Component classes: Components are elementary UI entities such as buttons, labels, text fields, and check boxes. Container classes: Containers, such as frames and panels, are used to hold UI components. A container can also hold containers.

Container vs. Component It is important to note that in a GUI program: a component must be kept in a container. You need to identify the container and component. Every container has a method called add(Component c). E.g.:- A container (says aContainer) can invoke aContainer.add(aComponent) to add aComponent into itself.

Frame & Panel Frame is the top-level container of an AWT GUI program. Frame has a title bar (containing program icon, title, minimize, maximize/restore-down and close buttons), an optional menu bar and program display area. Panel is an invisible rectangular box used to group related UI component. I

AWT Container Classes A GUI program must begin with a top-level container. The commonly-used top-level containers in AWT are Frame, Dialog and Applet. An AWT Frame is a "window”

AWT Container Classes An AWT GUI program is usually written as a subclass of java.awt.Frame class. An AWT Dialog is a pop-up window used for interacting with the users. A Panel is an invisible retangular box under a higher-level container, used to group and layout a set of related UI components

Hierarchy of the AWT Container classes

Component classes AWT provides many ready-made and reusable UI components. The frequently-used are: Button, TextField, Label, Checkbox, CheckboxGroup (radio buttons), List, and Choice

Component creation Three steps are necessary to create and place a UI component: Declare the component with an identifier; Construct via new operator and invoke the chosen constructor; Identify the container (such as Frame or Panel) designed to hold this component. The container can then add this component into itself via aContainer.add(aComponent) method. Every container has a add(Component) method.

Designing GUI GUI components are associated with events. Separate the program GUI classes from the program processing classes. Use one class to present the graphics, another class to process the data/user input, and one class to handle the events that occur with user interaction.

Java APIs for GUI The Java API provides a rich selection of classes to create the visual elements of a GUI and its functional facilities. This part explores the AWT and the Swing API of classes.

Java APIs for GUI There are two sets of Java APIs for GUI programming: AWT (Abstract Windowing Toolkit) and Swing. AWT APIs were introduced in JDK 1.0. (java.awt and javax.swing) Swing APIs, a much more comprehensive set of graphics libraries that enhances the AWT counterparts, has been integrated into core Java since JDK 1.2. These provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications.

GUI components Components form a major part of a GUI. Java components are pre-defined standard elements such as buttons, text-fields, frames, windows, labels, scrollbars, menus, menu items, text areas, and dialog boxes. The display space on the screen is also a component.

GUI components Like all GUI-based applications, the Java display space is a window. Some windows have additional graphics features and are known as frame windows. Frame windows have a title, a border, and buttons for closing, minimizing, and maximizing the window.

GUI components Some components are used to hold other components. For example, a dialog box can hold a label and a button. A window can hold a dialog box and other buttons and components. Components that can hold other components are called containers.

AWT & Swing packages These contain all of the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called a component.

AWT & Swing packages The Component class is the root of all AWT components. All AWT components share the properties described in the Component class.

AWT & Swing packages A container is a component that can contain components and other containers. A container usually has a layout manager that controls the visual placement of components in a container. The AWT package contains several layout manager classes

Class Hierarchy

Container vs. Component

Container vs. Component It is important to note that in a GUI program: a component must be kept in a container. You need to identify the container and component. Every container has a method called add(Component c). E.g.:- A container (says aContainer) can invoke aContainer.add(aComponent) to add aComponent into itself.

Java API – Graphics class The Graphics class has methods to draw lines, rectangles, and ovals as basic shapes. From these basic shapes, a programmer can create many complex graphics. GUI components are drawn on the screen using Graphics objects as well as the paint, repaint, and update methods. The Applet class also uses these methods to display the applet graphics in a browser window.

Common AWT Components A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples: buttons, checkboxes, and scrollbars of a typical graphical user interface. The Component class is the abstract superclass of the Abstract Window Toolkit components. The basic steps involved in using a GUI component are: The component object must be created with a constructor. It must be added to a container.

Button A Button component creates a labeled button. An object of class Button is a push button. The application can cause some action to happen when the button is pushed. The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent The constructor of Button class: Button(String label)

Example: Button Button quitButton = new Button(“Quit”); setLabel() and getLabel(): Sets and gets the label associated with the button.

Label A Label component is a single line of text. The text cannot be edited by the user, although it can be changed by your program. A Label class has two main constructors: Label name = new Label(“John"); //This Label constructor displays the given text. Label name = new Label(“John", Label.CENTER); //creates a label whose text is centered in the available space. getText() and setText(): Sets and gets the text for the label. name.setText(“Harry");

Example: Label setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); add(new Label("Hi There!")); add(new Label("Another Label")); (From Java API Docs)

Standard Component methods comp.setBackground(color) and comp.setForeground(color): Sets the background and foreground colors for the component. If no colors are set for a component, it inherits the colors of its parent container. comp.setFont(font): Sets the font for the component. That font is used to display text on the component. comp.getSize(): Returns the size of the component in the form of a Dimension object. When a component is first created, its size is zero. The size will be set later, probably by a layout manager.

comp. getParent(): Returns the parent Container of the component comp.getParent(): Returns the parent Container of the component. For a top-level component such as a Window or Applet, the return value will be null. comp.getLocation(): Gets the location of this component in the form of a point specifying the component's top-left corner. The location will be relative to the parent's coordinate space. comp.isEnabled(): Determines whether the component is enabled. An enabled component can respond to user input. Components are enabled initially by default. comp.setVisible(boolean b): Shows or hides the component depending on the value of parameter b. If true, shows the component; otherwise, hides the component.

AWT Container Classes A GUI program must begin with a top-level container. The commonly-used top-level containers in AWT are Frame, Dialog and Applet. An AWT Frame is a "window”

AWT Container Classes An AWT GUI program is usually written as a subclass of java.awt.Frame class. An AWT Dialog is a pop-up window used for interacting with the users. A Panel is an invisible retangular box under a higher-level container, used to group and layout a set of related UI components

Component creation Three steps are necessary to create and place a UI component: Declare the component with an identifier; Construct via new operator and invoke the chosen constructor; Identify the container (such as Frame or Panel) designed to hold this component. The container can then add this component into itself via aContainer.add(aComponent) method. Every container has a add(Component) method.

Event Handling GUIs are event driven Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc. Class java.awt.AWTEvent

Event Handling Models Event Inheritance Delegation

Some event classes of package java.awt.event Object EventObject AWTEvent ActionEvent AdjustmentEvent ItemEvent TextEvent ContainerEvent FocusEvent PaintEvent WindowEvent InputEvent MouseWheelEvent ComponentEvent KeyEvent MouseEvent Object ActionEvent EventObject AdjustmentEvent AWTEvent ContainerEvent ItemEvent FocusEvent TextEvent PaintEvent ComponentEvent WindowEvent InputEvent KeyEvent MouseEvent MouseWheelEvent

Event Handling Event-handling model Programmer must perform two tasks Three parts Event source GUI component with which user interacts Event object Encapsulates information about event that occurred Event listener (handler) Receives event object when notified, then responds Programmer must perform two tasks Register event listener for event source Implement event-handling method (event handler)

Event-listener interfaces of package java.awt.event ActionListener AdjustmentListener ComponentListener ContainerListener FocusListener ItemListener KeyListener MouseListener MouseMotionListener TextListener WindowListener «interface» EventListener «interface» ActionListener «interface» AdjustmentListener «interface» ComponentListener «interface» ContainerListener «interface» FocusListener «interface» ItemListener «interface» KeyListener «interface» MouseListener «interface» MouseMotionListener «interface» TextListener

Event model Components that react to user input can generate events Events are objects that represent what happened Objects that are designed to process the event register as a listener with the event generator

Events & listeners Event objects represent the event Subclass from java.awt.AWTEvent ActionEvent, TextEvent, KeyEvent, InputEvent Objects designed to process events are called handlers Handlers implement the appropriate listener interface Register with the event generator

Listener classes Separate listener interface for each event type ActionListener, ItemListener, FocusListener, etc The class that will be receiving the event implements the appropriate listener The action is performed in the method specified by the interface

Adapter classes Some listener interfaces include many methods To implement these interfaces the handler class must implement all these methods Java provides adapter classes, that implement empty methods The handler need only extend the adapter class and implement the method(s) of interest