Unit-Iv Awt Notes
Unit-Iv Awt Notes
Syllabus:
Java AWT:
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
The AWT tutorial will help the user to understand Java GUI programming in
simple and easy steps.
Java AWT calls the native platform calls the native platform (operating
systems) subroutine for creating API components like TextField, ChechBox,
button, etc.
For example, an AWT GUI with components like TextField, label and button
will have different look and feel for the different platforms like Windows, MAC
OS, and Unix. The reason for this is the platforms have different view for their
native components and AWT directly calls the native subroutine that creates
those components.
AWT Classes :
The AWT classes are contained in the java.awt package. It is one of Java’s
largest packages
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Component:
->. All user interface elements that are displayed on the screen and that interact
with the user are subclasses of Component.
->It defines over a hundred public methods that are responsible for managing
events, such as mouse and keyboard input, positioning and sizing the window,
and repainting
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Container :
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog
Panel :
The Panel is the container that doesn't contain title bar, border or menu bar. It is
generic container for holding the components. It can have other components like
button, text field ,
The Panel class is a concrete subclass of Container. It doesn’t add any new
methods; it simply implements Container. A Panel may be thought of as a
recursively nestable, concrete screen component. Panel is the superclass for
Applet.
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Other components can be added to a Panel object by its add( ) method (inherited
from Container). Once these components have been added, you can position and
resize them manually using the setLocation( ), setSize( ), setPreferredSize( ), or
setBounds( ) methods defined by Component
Window:
Frame :
Canvas:
Although it is not part of the hierarchy for applet or frame windows, there is
one other type of window that you will find valuable: Canvas. Canvas
encapsulates a blank window upon which you can draw
Frame( )
Frame(String title)
The first form creates a standard window that does not contain a title. The
second form creates a window with the title specified by title.
The setSize( ) method is used to set the dimensions of the window. Its signature
is shown here: void setSize(int newWidth, int newHeight)
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
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.
After a frame window has been created, it will not be visible until you call
setVisible( ). Its signature is shown here: void setVisible(boolean visibleFlag)
You can change the title in a frame window using setTitle( ), which has this
general form: void setTitle(String newTitle)
Frames Creations:
By inheritance:
By Association:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Label:
The object of the Label class is a component for placing text in a container. It is
used to display a single line of read only text. The text can be changed by a
programmer but a user cannot edit it directly.
It is called a passive control as it does not create any event when it is accessed.
To create a label, we need to create the object of Label class.
1. static int LEFT: It specifies that the label should be left justified.
2. static int RIGHT: It specifies that the label should be right justified.
3. static int CENTER: It specifies that the label should be placed in center.
Button:
TextField:
The object of a TextField class is a text component that allows a user to enter a
single line text and edit it.
When we enter a key in the text field (like key pressed, key released or key
typed), the event is sent to TextField. Then the KeyEvent is passed to the
registered KeyListener. It can also be done using ActionEvent; if the
ActionEvent is enabled on the text field, then the ActionEvent may be fired by
pressing return key. The event is handled by the ActionListener interface
l) events anymore.
Method Inherited
The AWT TextField class inherits the methods from below classes:
1. java.awt.TextComponent
2. java.awt.Component
3. java.lang.Object
ON
Code:
package test1;
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample2 extends Frame implements ActionListener {
TextField tf1, tf2, tf3;
Button b1, b2;
TextFieldExample2() {
tf1 = new TextField();
tf1.setBounds(50, 50, 150, 20);
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
TextArea:
The object of a TextArea class is a multiline region that displays text. It allows
the editing of multiple line text. It inherits TextComponent class.
The text area allows us to type as much text as we want. When the text in the
text area becomes larger than the viewable area, the scroll bar appears
automatically which helps us to scroll the text up and down, or right and left.
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Class constructors:
scrollbars) visibility.
Methods Inherited
o java.awt.TextComponent
o java.awt.Component
o java.lang.Object
11. void insert(String str, int pos) It inserts the specified text at the
specified position in this text
area.
package test1;
import java.awt.*;
import java.awt.event.*;
public class TextAreaExample2 extends Frame implements ActionListener {
Label l1, l2;
TextArea area;
Button b;
TextAreaExample2() {
l1 = new Label();
l1.setBounds(50, 50, 100, 30);
l2 = new Label();
l2.setBounds(160, 50, 100, 30);
area = new TextArea();
area.setBounds(20, 100, 300, 300);
b = new Button("Count Words");
b.setBounds(100, 400, 100, 30);
b.addActionListener(this);
add(l1); add(l2);add(area);
add(b);
setSize(400, 450);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
//generating event text area to count number of words and characters
public void actionPerformed(ActionEvent e) {
String text = area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
//main method
public static void main(String[] args) {
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
new TextAreaExample2();
}
}
Output:
CheckBox:
o java.awt.Component
o java.lang.Object
package test1;
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
import java.awt.*;
import java.awt.event.*;
public class CheckboxExample2
{
CheckboxExample2() {
Frame f = new Frame ("CheckBox Example");
final Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java");
checkbox2.setBounds(100, 150, 50, 50);
f.add(checkbox1);f.add(checkbox2);f.add(label);
checkbox1.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
label.setText("c++
CheckBox:"+(e.getStateChange()==1?"changed":"unchanged"));
}
});
checkbox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label.setText("Java Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
}
});
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
f.dispose();
}
});
}
// main method
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Output:
CheckboxGroup:
package test1;
import java.awt.*;
import java.awt.event.*;
public class CheckboxGroupExample
{
CheckboxGroupExample(){
Frame f= new Frame("CheckboxGroup Example");
final Label label = new Label();
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
label.setAlignment(Label.CENTER);
label.setSize(400,100);
CheckboxGroup cbg = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("C++", cbg, false);
checkBox1.setBounds(100,100, 50,50);
Checkbox checkBox2 = new Checkbox("Java", cbg, false);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1); f.add(checkBox2); f.add(label);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label.setText("C++ checkbox: Checked");
}
});
checkBox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label.setText("Java checkbox: Checked");
}
});
}
public static void main(String args[])
{
new CheckboxGroupExample();
}
}
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES 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.
o java.awt.Component
o java.lang.Object
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
12. void insert(String item, int index) Inserts the item into this
choice at the specified
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
position.
package test1;
import java.awt.*;
import java.awt.event.*;
// main method
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Output:
Canvas:
he Canvas class controls and represents a blank rectangular area where the
application can draw or trap by the user.
Class methods
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
The Canvas has inherited above methods from the following classes:
o lang.Component
o lang.Object
package test1;
import java.awt.*;
public class CanvasExample
{
public CanvasExample()
{
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
// adding specifications
g.setColor(Color.orange);
g.fillOval(75, 75, 150, 75);
}
}
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Panel:
Dialog:
The Dialog control represents a top level window with a border and a title used
to take some form of input from the user. It inherits the Window class.
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize and
minimize buttons but Dialog doesn't have.
Example On Dialog
The object of MenuItem class adds a simple labeled menu item on menu. The
items used in a menu must belong to the MenuItem or any of its subclass.
The object of Menu class is a pull down menu component which is displayed on
the menu bar. It inherits the MenuItem class.
package test1;
import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Scrollbar:
The object of Scrollbar class is used to add horizontal and vertical scrollbar.
Scrollbar is a GUI component allows us to see invisible number of rows and
columns.
ADVERTISEMENT
ADVERTISEMENT
20. void setValue (int newValue) It sets the value of scroll bar
with the given argument
value.
22. void setValues (int value, int It sets the values of four
visible, int minimum, int properties for scroll bar:
maximum) value, visible amount,
minimum and maximum.
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
package test1;
import java.awt.*;
import java.awt.event.*;
public class ScrollbarExample2 {
// class constructor
ScrollbarExample2() {
// creating a Frame with a title
Frame f = new Frame("Scrollbar Example");
// creating a final object of Label
final Label label = new Label();
// setting alignment and size of label object
label.setAlignment(Label.CENTER);
label.setSize(400, 100);
// creating a final object of Scrollbar class
final Scrollbar s = new Scrollbar();
// setting the position of scroll bar
s.setBounds(100, 100, 50, 100);
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
// main method
public static void main(String args[]){
new ScrollbarExample2();
}
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Java LayoutManagers
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
Java BorderLayout
Java GridLayout
The GridLayout() constructor creates only one row. The following example
shows the usage of the parameterless constructor.
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one
after another (in a flow). It is the default layout of the applet or panel.
Java CardLayout
The Java CardLayout class manages the components in such a manner that
only one component is visible at a time. It treats each component as a card that
is why it is known as CardLayout.
The following program uses the next() method to move to the next card of the
container.
package test1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
{
cPane = getContentPane();
crd = new CardLayout();
cPane.setLayout(crd);
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
btn3 = new JButton("Cat");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
cPane.add("a", btn1);
cPane.add("b", btn2);
cPane.add("c", btn3);
}
public void actionPerformed(ActionEvent e)
{
crd.next(cPane);
}
public static void main(String argvs[])
{
CardLayoutExample1 crdl = new CardLayoutExample1();
crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
Java GridBagLayout
The components may not be of the same size. Each GridBagLayout object
maintains a dynamic, rectangular grid of cells. Each component occupies one or
more cells known as its display area. Each component associates an instance of
GridBagConstraints. With the help of the constraints object, we arrange the
component's display area on the grid. The GridBagLayout manages each
component's minimum and preferred sizes in order to determine the
component's size. GridBagLayout components are also arranged in the
rectangular grid but can have many different sizes and can occupy multiple
rows or columns.
Code:
package test1;
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends JFrame
{
public static void main(String[] args)
{
GridBagLayoutExample a = new GridBagLayoutExample();
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT
}
public GridBagLayoutExample()
{
GridBagLayout GridBagLayoutgrid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(GridBagLayoutgrid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 40;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:
CMRCET CSE JAVA UNIT IV PART -1 NOTES JAVA AWT