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.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
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,
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
MCA-401: ADVANCED JAVA PROGRAMMING 1DEPTT. OF COMP. SC & APPLICATIONS PREPARED BY : NAVEEN NAGPAL (SENIOR ASSISTANT PROFESSOR)
Objectives of This Session
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.
GUI Graphical User Interface. Outline Introduction Packages Components Layout Manager Events and Listeners Examples.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
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.
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.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
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 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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 (By Rama Bhadra Rao M) You can Trace me in Day 5
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Aum Amriteshwaryai Namah
Advanced GUIs II CS Lecture
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Java Swing.
AWT Components.
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT.
AWT Components and Containers
Graphics Programming - Frames
Topics Graphical User Interfaces Using the tkinter Module
Constructors, GUI’s(Using Swing) and ActionListner
Menu Bars and Menus.
GUI building with the AWT
Objectives The objectives of this chapter are:
Advanced GUIs II CS Lecture
A picture's worth a thousand words
Advanced GUIs and Graphics
Presentation transcript:

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. its components are using the resources of OS. The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy The hierarchy of Java AWT classes are given below. Note: There are more classes, however, these are what are covered in this chapter

Component Component is the superclass of most of the displayable classes defined within the AWT. Note: it is abstract. MenuComponent is another class which is similar to Component except it is the superclass for all GUI items which can be displayed within a drop-down menu. The Component class defines data and methods which are relevant to all Components setBounds setSize setLocation setFont setEnabled setVisible setForeground -- color setBackground -- color The Component class contains the common features to all items which can be displayed in a GUI. Often, these items are called “widgets”. In the AWT, all widgets are components and, as such, inherit all the data and methods of the Component class.

Container The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. For a component to be placed on the screen, it must be placed within a Container The Container class defined all the data and methods necessary for managing groups of Components add getComponent getMaximumSize getMinimumSize getPreferredSize remove removeAll

Window The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. Top level window without borders, menubar show() makes window visible toFront(), toBack() pack() resizes window to fit components in it dispose() frees resources when window is not needed Example use: pop-up menu

Panel The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. Alternative to window e.g., for display of Applet in Web browser A Panel is a rectangular Container whose sole purpose is to hold and manage components within a GUI. Panel p1 = new Panel(); p1.add(new Button("Ok")); aPanel.add(new Button("Cancel")); Frame f = new Frame("Button Test"); f.setSize(100,100); f.setLocation(10,10); f.add(p1);

Frame The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc. A window with title, menubar, icon, cursor

Useful Methods of Component class Description public void add(Component c) inserts a component on this component. public void setSize(int width,int height) sets the size (width and height) of the component. public void setLayout(LayoutManager m) defines the layout manager for the component. public void setVisible(boolean status) changes the visibility of the component, by default false.

AWT Components

Using AWT Components Component Component MenuComponent TextComponent Canvas Scrollbar Button Checkbox Label List Choice TextComponent TextArea TextField Component Container Panel Window Dialog FileDialog Frame MenuComponent MenuItem Menu

Frame import java.awt.*; public class TestFrame extends Frame { public TestFrame(String title){ super(title); } public static void main(String[] args){ Frame f = new TestFrame("TestFrame"); f.setSize(400,400); f.setLocation(100,100); f.show();

How to Use Buttons? Button This class represents a push-button which displays some specified text. When a button is pressed, it notifies its Listeners. (More about Listeners in the next chapter). To be a Listener for a button, an object must implement the ActionListener Interface. public class TestButton extends Frame { public TestButton(String title){ super(title); Button hw = new Button("Hello World!"); add(hw); } ….

Label Displays 1 line of text (read-only) This class is a Component which displays a single line of text. Labels are read-only. That is, the user cannot click on a label to edit the text it displays. Text can be aligned within the label

How to Use Labels? public class TestLabel extends Frame { public TestLabel(String title){ super(title); Label label1 = new Label(); label1.setText("Label1"); Label label2 = new Label("Label2"); label2.setAlignment(Label.CENTER); Label label3 = new Label("Label3");   add(label1,"North"); add(label2,"Center"); add(label3,"South"); }

Checkbox Checkboxgroup This class represents a GUI checkbox with a textual label. The Checkbox maintains a boolean state indicating whether it is checked or not. Checkboxgroup If a Checkbox is added to a CheckBoxGroup, it will behave like a radio button.

How to Use Checkboxes? public class TestCheckbox extends Frame { public TestCheckbox(String title){ super(title);   CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("American Express",cbg,false); Checkbox cb2 = new Checkbox("Visa",cbg,false); Checkbox cb3 = new Checkbox("Mastercard",cbg,true); add(cb1,"North"); add(cb2,"Center"); add(cb3,"South"); } …

 Choice This class represents a dropdown list of Strings. Similar to a list in terms of functionality, but displayed differently. Only one item from the list can be selected at one time and the currently selected element is displayed. 

How to Use Choices? public class TestChoice extends Frame { public TestChoice(String title){ super(title);   Choice choice = new Choice(); choice.add("ichi"); choice.add("ni"); choice.add("san"); add(choice); }

TextComponent This class displays optionally editable text. This class inherits several methods from TextComponent. This is one of the most commonly used Components in the AWT Can be editable (getText() returns text) Can be selectable TextField One line TextComponent setEchoCharacter() allows password entry TextArea Multiline TextComponent

How to Use TextField & TextArea public class TestText extends Frame { public TestText(String title){ super(title);   TextField textField = new TextField(20); TextArea textArea = new TextArea(5, 20); textArea.setEditable(false); textField.setText("TextField"); textArea.setText("TextArea Line1 \n TextArea Line2"); add(textField,"North"); add(textArea,"South"); } …

List Displays list of strings as N rows Adds scrollbar if necessary Allows single and multiple selection Method to return selected item indexes

How to Use Lists? public class TestList extends Frame { public TestList(String title){ super(title); List l = new List(2, true); //prefer 2 items visible l.add("zero"); l.add("uno"); l.add("dos"); l.add("tres"); l.add("cuatro"); l.add("cinco"); l.add("seis"); l.add("siete"); l.add("ocho"); l.add("nueve"); add(l); }

Scrollbar You specify orientation (horz, vert, and min/max range) Event returned specifies #lines or pages to scroll, or absolute position to scroll to

Scrollbars and Sliders Constructors Scrollbar Creates a vertical scrollbar The “bubble” (or “thumb,” the part that actually moves) size defaults to 10% of the trough length The internal min and max values are set to zero Scrollbar(int orientation) Similar to above; specify a horizontal (Scrollbar.HORIZONTAL) or vertical (Scrollbar.VERTICAL) scrollbar Scrollbar(int orientation, int initialValue, int bubbleSize, int min, int max) Creates a horizontal or vertical “slider” for interactively selecting values Specify a customized bubble thickness and a specific internal range of values Bubble thickness is in terms of the scrollbar’s range of values, not in pixels, so if max minus min was 5, a bubble size of 1 would specify 20% of the trough length

Scollbars: Example public class Scrollbars extends Applet { public void init() { int i; setLayout(new GridLayout(1, 2)); Panel left = new Panel(), right = new Panel(); left.setLayout(new GridLayout(10, 1)); for(i=5; i<55; i=i+5) { left.add(new Scrollbar(Scrollbar.HORIZONTAL, 50, i, 0, 100)); } right.setLayout(new GridLayout(1, 10)); right.add(new Scrollbar(Scrollbar.VERTICAL, add(left); add(right);

How to Use Canvases and Graphics Primitives? For drawing geometric shapes, texts, and images An abstract class the extended class must override paint() Line RoundRectangle Polygon Oval Rectangle Arc

drawLine(x1,y1,x2,y2) class MyCanvas extends Canvas { public void paint(Graphics g){ g.setColor(Color.blue); int x1 = 161, y1 = 186, x2 = 181, y2 = 206; g.drawLine(x1,y1,x2,y2); } (x1,y1) (x2,y2)

fillOval(x,y,w,h) drawOval(x,y,w,h) g.setColor(Color.blue); { int x = 239, y = 186, w = 48, h = 32; g.fillOval(x,y,w,h); }

fillPolygon(int[] xs, int[] ys) drawPolygon(int[] xs, int[] ys) g.setColor(Color.green); { int xs[] = {161,161,185,209,185,161}; int ys[] = {310,334,358,334,310,310}; g.fillPolygon(xs,ys,6); }

fillRect(x,y,w,h) drawRect(x,y,w,h) g.setColor(Color.pink); { int x = 239, y = 248, w = 48, h = 32; g.fillRect(x,y,w,h); }

fillRoundRect(x,y,w,h,rw,rh) drawRoundRect(x,y,w,h,rw,rh) g.setColor(Color.yellow); { int x = 161, y = 248, w = 48, h = 32, roundW = 25, roundH = 25; g.fillRoundRect(x,y,w,h,roundW,roundH); }

fillArc(x,y,w,h,sa,a) drawArc(x,y,w,h,sa,a) g.setColor(Color.orange); { int x = 239, y = 310, w = 48, h = 48, startAngle = 20, angle = 90; g.fillArc(x,y,w,h,startAngle,angle); }