AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.

Slides:



Advertisements
Similar presentations
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Advertisements

Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Components. Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent –TextArea –TextField.
Topics AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
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: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
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.
1 Lecture 6 Using AWT controls, Layout Managers, and Menus.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Using AWT Controls, Layout Managers, and Menus Controls are components that allow a user to interact with your application in various ways A layout manager.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Csc Basic Graphical User Interface (GUI) Components.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Event and GUI programming 1. Event Handling in Java An object resides in a particular state until it is made to transit to other state. This transition.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
CSC 205 Programming II Lecture 5 AWT - I.
Fundamental of Java Programming Abstract Window Toolkit
Aum Amriteshwaryai Namah
Advanced GUIs II CS Lecture
Java Swing.
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT Components and Containers
Graphics Programming - Frames
Programming Graphical User Interface (GUI)
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
Advanced GUIs II CS Lecture
A picture's worth a thousand words
Advanced GUIs and Graphics
Presentation transcript:

AWT Package

Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program is a set of nested components starting from outermost window all the way down to the smallest UI components. A graphical Java program is a set of nested components starting from outermost window all the way down to the smallest UI components. Java awt package provides following: Java awt package provides following: A full set of UI widgets and other components including windows, menus, buttons, check boxes, text fields, scroll bars, etc. A full set of UI widgets and other components including windows, menus, buttons, check boxes, text fields, scroll bars, etc. Support for UI containers which can contain other embedded containers or UI widgets. Support for UI containers which can contain other embedded containers or UI widgets. An event system for managing system and user events among parts of the AWT. An event system for managing system and user events among parts of the AWT. Mechanisms for laying out components in a way that enable platform independent UI design. Mechanisms for laying out components in a way that enable platform independent UI design.

Windows Fundamental Component Canvas Container TextComponentButton Panel WindowTextField AppletFrame Dialog

The most commonly used windows are those derived from Panel which used by Applet, and The most commonly used windows are those derived from Panel which used by Applet, and Those derived from Frame which creates a standard window. Those derived from Frame which creates a standard window.

Component Abstract class that encapsulates all of the attributes of a visual component. Abstract class that encapsulates all of the attributes of a visual component. It defines over a hundred public methods that are responsible for managing events such as mouse, keyboard input, positioning and sizing the window and repainting. It defines over a hundred public methods that are responsible for managing events such as mouse, keyboard input, positioning and sizing the window and repainting. A component object is responsible for remembering the current foreground and background colors and currently selected text font. A component object is responsible for remembering the current foreground and background colors and currently selected text font.

Container It is a subclass of Component. It is a subclass of Component. A container is responsible for laying out ( i.e. positioning) any components that it containers. A container is responsible for laying out ( i.e. positioning) any components that it containers. It does this through the use of the various layout managers. It does this through the use of the various layout managers.

Panel It is a concrete subclass of Container. It is a concrete subclass of Container. It doesn’t add any new methods. It doesn’t add any new methods. It simply implements Container. It simply implements Container. A Panel may be thought of as a recursively nestable, concrete screen component. A Panel may be thought of as a recursively nestable, concrete screen component. Panel is the superclass of Applet. Panel is the superclass of Applet. A panel is a window that doesn’t contain a title bar, menu bar or border. A panel is a window that doesn’t contain a title bar, menu bar or border. This is why you don’t see these items when an applet is run inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border. This is why you don’t see these items when an applet is run inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border.

Other components can be added to a Panel object by its add() method [ inherited from container ] Other components can be added to a Panel object by its add() method [ inherited from container ] Once these components have been added, they can be positioned and resized manually using the setLocation(), setSize(), or setBounds() methods defined by Component. Once these components have been added, they can be positioned and resized manually using the setLocation(), setSize(), or setBounds() methods defined by Component.

Window Creates a top-level window. Creates a top-level window. A top level window is not contained within any other object. A top level window is not contained within any other object. Generally window objects are not created directly instead a subclass of Window called Frame is used. Generally window objects are not created directly instead a subclass of Window called Frame is used.

Frame it is a subclass of Window and has a title bar, menu bar, borders and resizing corners. it is a subclass of Window and has a title bar, menu bar, borders and resizing corners.

Canvas It encapsulates a blank window which can be drawn upon. It encapsulates a blank window which can be drawn upon. Good for painting images and performing other graphic operations. Good for painting images and performing other graphic operations.

Basic UI Components Labels Labels Buttons Buttons Check boxes Check boxes Radio buttons Radio buttons Choice menus / list Choice menus / list Text fields Text fields Text area Text area Scrolling list Scrolling list scrollbars scrollbars

Basic procedure to create these UI Basic procedure to create these UI Create the component Create the component Add it to panel or applet Add it to panel or applet

Labels Constructors Constructors Label()creates an empty label Label()creates an empty label Label(String)creates a label with the given text string aligned left Label(String)creates a label with the given text string aligned left Label(String,int)creates a label with the given string and int alignment i.e. Label(String,int)creates a label with the given string and int alignment i.e. Label.RIGHT Label.RIGHT Label.LEFT Label.LEFT Label.CENTER Label.CENTER

Lable’s font can be changed by setFont() method of label class. Lable’s font can be changed by setFont() method of label class. Label lblName = new Label(“Label Name”); add(lblName);

import java.awt.*; public class LabelTest extends java.applet.Applet{ LabelTest public void init(){ setFont(new Font(“Helvetica”,Font.BOLD,14)); Label lblLeft=new Label(“LEFT”,Label.LEFT); add(lblLeft); Label lblRight=new Label(“RIGHT”,Label.RIGHT); add(lblRight); } } //============================= Applet Applet Applet <body>

Methods of Label getText() getText() Returns a string containing this label’s text Returns a string containing this label’s text setText(String) setText(String) Changes the text of the label Changes the text of the label getAlignment() getAlignment() Returns an integer 0=Label.LEFT 1=Label.CENTER 2=Label.RIGHT Returns an integer 0=Label.LEFT 1=Label.CENTER 2=Label.RIGHT setAlignment(int) setAlignment(int) Changes the alignment of the label. Changes the alignment of the label.

Buttons Constructors Constructors Button()button with no label Button()button with no label Button(String)button with label Button(String)button with label add() used to add button on applet. add() used to add button on applet. getLabel() getLabel() returns a label of button returns a label of button setLabel(String) setLabel(String) add or change the label of button add or change the label of button

Check Boxes It has two states – on (checked/selected/true) or off (unchecked/unselected/false) It has two states – on (checked/selected/true) or off (unchecked/unselected/false) Used in 2 ways Used in 2 ways Exclusive Exclusive Given a series of checkboxes, only 1 check box can be selected at a time Given a series of checkboxes, only 1 check box can be selected at a time Nonexclusive Nonexclusive Given a series of check boxes any of them can be selected. Given a series of check boxes any of them can be selected.

Nonexclusive check boxes can be created using the “Checkbox” class. Nonexclusive check boxes can be created using the “Checkbox” class. Exclusive check boxes are created by first creating a Radio Group and then assigning check boxes to the Radio Group. Exclusive check boxes are created by first creating a Radio Group and then assigning check boxes to the Radio Group.

Constructors Constructors Checkbox()empty checkbox, unselected Checkbox()empty checkbox, unselected Checkbox(String)checkbox with label Checkbox(String)checkbox with label Checkbox(String,boolean) checkbox with label and selected / deselected depending upon true / false. Checkbox(String,boolean) checkbox with label and selected / deselected depending upon true / false. Checkbox(String,null,boolean) Checkbox(String,null,boolean) getLabel()returns a string containing the check box label getLabel()returns a string containing the check box label setLabel(String)Changes checkbox label setLabel(String)Changes checkbox label getState()returns true / false based on whether checkbox is selected getState()returns true / false based on whether checkbox is selected setState(boolean)changes the state of checkbox setState(boolean)changes the state of checkbox

public class CheckboxTest extends Applet{ public void init(){ Checkbox chkShoe=new Checkbox(“Shoe”); add(chkShoe); Checkbox chkSock=new Checkbox(“Sock”,true); add(chkSock);}}

Radio Buttons It is hollow round It is hollow round To create a series of radio buttons an instance of CheckboxGroup must first be created. To create a series of radio buttons an instance of CheckboxGroup must first be created. CheckboxGroup chkgrp=new CheckboxGroup(); CheckboxGroup chkgrp=new CheckboxGroup(); After this, create and add individual radio buttons from the checkbox class as follows- After this, create and add individual radio buttons from the checkbox class as follows- add( newCheckbox( String,CheckboxGroup,boolean)); add( newCheckbox( String,CheckboxGroup,boolean)); getCheckboxGroup() getCheckboxGroup() to access the group to access the group setCheckboxGroup() setCheckboxGroup() to change the group to change the group getSelectedCheckbox() getSelectedCheckbox() gets the selected checkbox gets the selected checkbox setSelectedCheckbox(Checkbox) setSelectedCheckbox(Checkbox) sets the given checkbox as selected sets the given checkbox as selected getCurrent() and setCurrent() can be used to get and set the currently selected check box. getCurrent() and setCurrent() can be used to get and set the currently selected check box.

Choice Menus / Lists Pull down list of items from which one item can be selected at a time. Pull down list of items from which one item can be selected at a time. Choice ch = new Choice(); ch.add(“apples”);ch.add(“oranges”);ch.add(“bananas”);add(ch);

Methods with Choice object Methods with Choice object getItem(int) getItem(int) Returns the string item at the given place; begins at 0. Returns the string item at the given place; begins at 0. getSelectedIndex() getSelectedIndex() Returns index position of selected item. Returns index position of selected item. getSelectedItem() getSelectedItem() Returns currently selected item as a string Returns currently selected item as a string select(int) select(int) Selects the item at given position Selects the item at given position select(String) select(String) Selects the item with the given string. Selects the item with the given string.

TextFields Constructors Constructors TextField() TextField() Empty text field which can be resized by the current layout manager. Empty text field which can be resized by the current layout manager. TextField(int) TextField(int) Empty text field with “int” width Empty text field with “int” width TextField(String) TextField(String) Text field with initialized string Text field with initialized string TextField(String, int) TextField(String, int)

getText() getText() returns text that the text field contains returns text that the text field contains setText(String) setText(String) Puts the string into the field Puts the string into the field setColumns() setColumns() Sets width Sets width select(int,int) select(int,int) Select the text between 2 integer positions (starting from 0) Select the text between 2 integer positions (starting from 0) selectAll() selectAll() Select all text Select all text isEditable() isEditable() Returns true / false whether text is editable Returns true / false whether text is editable setEditable(boolean) setEditable(boolean) True(default) to edit, false to freeze text True(default) to edit, false to freeze text getEchoChar(char) getEchoChar(char) Returns the character used for masking input Returns the character used for masking input setEchoChar(char) setEchoChar(char) Set the character used for masking input Set the character used for masking input echoCharIsSet() echoCharIsSet() Returns true / false whether field has echo masking character set. Returns true / false whether field has echo masking character set.

Text Area Constructors: Constructors: TextArea()creates an empty text area TextArea()creates an empty text area TextArea(int no_of_lines,int width) TextArea(int no_of_lines,int width) TextArea(String) TextArea(String) TextArea(String,int,int) TextArea(String,int,int) String letter=“ad,adksdskj akdkl \n” + “sndsakjd andkajsd jsdkjkdnkjskddsk jsdk \n”+ “asdj hdbjsa jhbsdjbh hd”; TextArea ta=new TextArea(letter,10,45); add(ta);

setText, getText, setEditable, isEditable can be used with TextArea also. setText, getText, setEditable, isEditable can be used with TextArea also. insertText(String, int) insertText(String, int) Inserts the string at the character index indicated by the integer. Inserts the string at the character index indicated by the integer. replaceText(String,int,int) replaceText(String,int,int) Replaces the text between the given integer position with the indicated string. Replaces the text between the given integer position with the indicated string.

Scrolling Lists More than one item can be selected at a time More than one item can be selected at a time Multiple items are displayed Multiple items are displayed Scroll bars can be used. Scroll bars can be used. Constructors: Constructors: List()empty scrolling list from which only one item is selected at a time. List()empty scrolling list from which only one item is selected at a time. List(int,boolean)scrolling list indicating no of items visible on list. Boolean shows whether multiple items can be selected or not. List(int,boolean)scrolling list indicating no of items visible on list. Boolean shows whether multiple items can be selected or not. add() is used. add() is used.

List lt=new List(3,true); lt.add(“MCA”);lt.add(“MBA”);lt.add(“BCA”);lt.add(“BBA”);add(lt);

addItem() to add item to list. addItem() to add item to list. getItem, countItems, getSelectedIndex, getSelectedItem, select work same as in chioce list. getItem, countItems, getSelectedIndex, getSelectedItem, select work same as in chioce list. getSelectedIndexes() getSelectedIndexes() Returns array of integers containing index position of each selected item. Returns array of integers containing index position of each selected item. getSelectedItems() getSelectedItems() Returns array of strings containing text of each selected item. Returns array of strings containing text of each selected item.

Scrollbars Constructors: Constructors: Scrollbar() Scrollbar() Vertical scroll bar with max and min value as 0. Vertical scroll bar with max and min value as 0. Scrollbar(int) Scrollbar(int) Same as above. “int” Scrollbar.HORIZONTAL, Scrollbar.VERTICAL Same as above. “int” Scrollbar.HORIZONTAL, Scrollbar.VERTICAL Scrollbar(int,int,int,int,int) Scrollbar(int,int,int,int,int) Orientation horizontal or vertical Orientation horizontal or vertical Initial value in between of max and min value Initial value in between of max and min value Overall width or height of the box Overall width or height of the box Min value Min value Max value Max value

getValue() getValue() Returns scroll bars current value Returns scroll bars current value setValue(int) setValue(int) Sets the current value for the scrollbar. Sets the current value for the scrollbar.

Working with Panels Panel class is subclass of Container. It simply implements Container. Panel class is subclass of Container. It simply implements Container. Panel is super class of Applet. Panel is super class of Applet. In essence, panel is a window that does not contain title bar, menu bar, border. In essence, panel is a window that does not contain title bar, menu bar, border. PanelApplet.java PanelApplet.java PanelApplet.java

Working with Frames Frame is used to create child windows within applets and top-level or child windows for application. Frame is used to create child windows within applets and top-level or child windows for application. Constructors: Constructors: Frame()window without title Frame()window without title Frame(String title)window with title Frame(String title)window with title After creation give size to window After creation give size to window Example Example Example

Methods of Frame dispose() to close frame window dispose() to close frame window getTitle() to get the title of frame window getTitle() to get the title of frame window isRsizeable() it takes a boolean value isRsizeable() it takes a boolean value setTitle(String str) setTitle(String str) FrameTest FrameTest FrameTest

Setting the window’s dimension void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels.

The getSize( ) method is used to obtain the current size of a window. Dimension getSize( ) This method returns the current size of the window contained within the width and height fields of a Dimension object.

Hiding and Showing a Window After a frame window has been created, it will not be visible until you call setVisible( ). void setVisible(boolean visibleFlag) The component is visible if the argument to this method is true. Otherwise, it is hidden. You can change the title in a frame window using setTitle( ) – void setTitle(String newTitle)

Closing a Frame Window When using a frame window, your program must remove that window from the screen when it is closed, by calling setVisible(false). To intercept a window-close event, you must implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), you must remove the window from the screen.