UNIT 1 - AWT (Abstract Windowing Toolkit)
UNIT 1 - AWT (Abstract Windowing Toolkit)
TOOLKIT(AWT)
12 Marks
UNIT 1- AWT(Abstract windowing toolkit)
Creating a Frame
There are two ways to create a Frame. They are,
• By Instantiating Frame class
• By extending Frame class
AWT Classes
AWT Classes
AWT Classes
AWT Classes
AWT Classes
➢Setting a Window’s Title
void setTitle(String newTitle)
Checkbox
Checkbox Group
Choice
List
Scrollbar
TextField
TextArea
LABEL
Labels are passive controls that do not support any
interaction with the user.
Constructors
Label( )
Label(String str)
Label(String str, int how)
Where as
str- String to display which is left aligned. how-
how the alignment. I
t’s value can be set as,
Label.LEFT, Label.RIGHT,
Label.CENTER
Methods
1) void setText(String str)
2) String getText()
• You can add the following method also for resizing the
frame - setResizable(true);
setBounds() method
Button( )
Button(String str)
Methods:
void setLabel(String str)
String getLabel()
Creating Frame window by extending Frame class
• import java.awt.Button;
• import java.awt.Frame;
•
3) Void select(int startindex,int endindex) : you can select portion of text under program
control.
Boolean isEditable()
• import java.awt.*;
• class TextFieldDemo1{
• public static void main(String args[])
• {
• Frame TextF_f= new Frame("studytonight ==>TextField");
• TextField text1,text2;
• text1=new TextField("Welcome to studytonight");
• text1.setBounds(60,100, 230,40);
• text2=new TextField("This tutorial is of Java");
• text2.setBounds(60,150, 230,40);
• TextF_f.add(text1);
• TextF_f.add(text2);
• TextF_f.setSize(500,500);
• TextF_f.setLayout(null);
• TextF_f.setVisible(true);
• }
• }
•
• AWT TextArea
• In Java, AWT contains aTextArea Class. It is
used for displaying multiple-line text.
TEXT AREA
It is a multi line editor byAWT.
Constructors:
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int
numChars)
TextArea(String str, int numLines, int
numChars, int sBars)
• import java.awt.*;
• public class TextAreaDemo1
• {
• TextAreaDemo1()
• {
• Frame textArea_f= new Frame();
• TextArea area=new TextArea("Welcome to studytonight.com");
• area.setBounds(30,40, 200,200);
• textArea_f.add(area);
• textArea_f.setSize(300,300);
• textArea_f.setLayout(null);
• textArea_f.setVisible(true);
• }
• public static void main(String args[])
• {
• new TextAreaDemo1();
• }
• }
•
• AWT Checkbox
• In Java, AWT contains a Checkbox Class. It is used
when we want to select only one option i.e true or
false. When the checkbox is checked then its state is
"on" (true) else it is "off"(false).
CHECK BOX
A check box is a control that is used to turn an option on
or off. It consists of a small box that can either contain a
check mark or not.
Constructors
Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
• package javacomponent;
• import java.awt.*;
• public class CheckBoxEx {
•
• }
CHECKBOX GROUP
It is used to create a set of mutually exclusive check
boxes in which one and only one checkbox in the
group can be checked at any one time. These check
boxes are often called radio buttons
Constructor
CheckboxGroup();
Methods
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
where as
which is the check box that you want to be selected.
• import java.awt.*;
• public class CheckboxGroupExample
• {
• CheckboxGroupExample()
• {
• Frame f= new Frame("CheckboxGroup Example");
• CheckboxGroup cbg = new CheckboxGroup();
• Checkbox checkBox1 = new Checkbox("Male", cbg, false);
• checkBox1.setBounds(100,100, 50,50);
• Checkbox checkBox2 = new Checkbox("Female", cbg, true);
• checkBox2.setBounds(100,150, 60,50);
• f.add(checkBox1);
• f.add(checkBox2);
• f.setSize(400,400);
• f.setLayout(null);
• f.setVisible(true);
• }
• public static void main(String args[])
• {
• new CheckboxGroupExample();
• }
• }
Java AWT Choice
• The object of Choice class is used to show popup menu of
choices. Choice selected by user is shown on the top of a
menu. It inherits Component class.
Constructor
• Choice();
Methods
•void add(String name)
String getSelectedItem( ) int
getSelectedIndex( ) int
getItemCount( )
• void select(int index)
• void select(String name)
• String getItem(int index)
• import java.awt.*;
• public class ChoicegrpEx
• {
•
List( )
List(int numRows)
List(int numRows, boolean multipleSelect)
Where as,
numRows=number of entries in the list multipleSelect=t
can be set as true or false. If true
user can select two or more items at a time.
Methods
1) void add(String name)
2) void add(String name, int index) : adds item at
the specified index.
3) String getSelectedItem() : returns String
containing the name of the item.
4) int getSelectedIndex() : It returns the index of
the item.
5) String[] getSelectedItems() : returns an array containing
the name of currently selected items.
6)Int[] getSelectedIndexes() : returns an array containing
the indexes of currently selected items.
• import java.awt.*; • // setting size, layout and visibility of frame
• • f.setSize(400, 400);
• public class ListExample1 • f.setLayout(null);
• { • f.setVisible(true);
• // class constructor • }
• ListExample1() { •
• // creating the frame • //main method
• Frame f = new Frame(); • public static void main(String args[])
• // creating the list of 5 rows • {
• List l1 = new List(5); • new ListExample1();
• • }
• }
• // setting the position of list component
• l1.setBounds(100, 100, 75, 75);
•
2) Scrollbar(int style)
• }
• }
LAYOUT MANAGER
Layout means the arrangement of components
• within the container.
The layout manager automatically positions all the
components within the container.
The layout manager is associated with every
Container object.
Each layout manager is an object of the class that
• implements the LayoutManager interface.
The layout manager is set by the setLayout( ) method
• void setLayout(LayoutManager layoutObj)
TYPES OF LAYOUTMANAGER
FlowLayout
BorderLayout
GridLayout
CardLayout
GridBagLayout
The constructors for FlowLayout are shownbelow:
1. FlowLayout( )
2. FlowLayout(int how)
It allows you to specify how each Line is aligned.The value of how
is
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
• import java.awt.*;
•
• }
•
• }
Grid Layout
• The Grid Layout class is used to arrange the
components in a rectangular grid.
• One component is displayed in each rectangle
• When you instantiate a GridLayout, you
define the number of rows and columns.
The constructors supported by GridLayout are
shown below:
➢ GridLayout( )
• It creates a single-column grid layout.
Constructor
GridBagLayout()
commonly Used Methods:
• }
DIALOGBOX
Dialog boxes are pop-up windows on the screen
that appear for a small time to take either input
or display output while a main application is
running.
A top-level pop-up window with a title and a
border that typically takes some form of input
from the user.
It is used to display message and get specific
• information from the user.
It is used to obtain user Input.
TYPES OF DIALOG BOXES
Modal dialog box does not allow the user todo any
activity without dismissing (closing) it; example is File
Deletion Confirmation dialog box
Modeless dialog box permits the user to do any
activity without closing it; example is Find and
Replace dialog box of MS Word. Java supports both
styles of dialog boxes.
CONSTRUCTOR
Dialog(Frame parent)
Dialog(Frame parent,Boolean mode)
• // Create an OK button
• Button ok = new Button ("OK");
FILEDIALOG
The FileDialog class displays a dialog window
from which the user can select a file.
To create a file dialog box ,object of type
FileDialog is instatiated,this display std file
dialog box
CONSTRUCTOR
FileDialog(Frame parent)
FileDialog(Frame parent,String title)
FileDialog(Frame parent,String title,int how) whereas
how is
1) FileDialog.SAVE
This constant value indicates that the purpose of the
file dialog window is to locate a file to which to write.
2) FileDialog.LOAD
This constant value indicates that the purpose of the
file dialog window is to locate a file from which to read.
METHODS
String getDirectory()Gets the directory of this file dialog.
String getFile() Gets the selected file of this file
dialog.
int getMode()Indicates whether this file dialog box
is for loading from a file or for saving to a file.