0% found this document useful (0 votes)
4 views

19 Java AWT

AWT (Abstract Window Toolkit) is a platform-dependent API for creating GUI components in Java, which results in different appearances across operating systems. It is less commonly used today in favor of Swing, which is platform-independent and offers a wider range of components. AWT includes various containers like Window, Frame, Dialog, and Panel to organize GUI components such as buttons and text fields.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

19 Java AWT

AWT (Abstract Window Toolkit) is a platform-dependent API for creating GUI components in Java, which results in different appearances across operating systems. It is less commonly used today in favor of Swing, which is platform-independent and offers a wider range of components. AWT includes various containers like Window, Frame, Dialog, and Panel to organize GUI components such as buttons and text fields.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java AWT

AWT stands for Abstract Window Toolkit. It is a platform dependent API for creating Graphical User Interface (GUI)
for java programs.

Java AWT calls native platform (Operating systems) subroutine for creating components such as textbox, checkbox,
button etc. For example an AWT GUI having a button would have a different look and feel across platforms like
windows, Mac OS & Unix, this is because these platforms have different look and feel for their native buttons and
AWT directly calls their native subroutine that creates the button.

AWT is rarely used now days because of its platform dependent and heavy-weight nature.

Swing is a preferred API for window based applications because of its platform independent and light-weight nature.
Swing is built upon AWT API however it provides a look and feel unrelated to the underlying platform. It has more
powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and
labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists.

Q. Differentiate AWT vs Swing in java.

AWT hierarchy

Components and containers


All the elements like buttons, text fields, scrollbars etc are known as components. In AWT we have classes for each
component as shown in the above diagram. To have everything placed on a screen to a particular position, we have
to add them to a container. A container is like a screen wherein we are placing components like buttons, text fields,
checkbox etc. In short a container contains and controls the layout of components. A container itself is a component
(shown in the above hierarchy diagram) thus we can add a container inside container.

Types of containers:
As explained above, a container is a place wherein we add components like text field, button, checkbox etc. There
are four types of containers available in AWT: Window, Frame, Dialog and Panel. As shown in the hierarchy diagram
above, Frame and Dialog are subclasses of Window class.

Window: An instance of the Window class has no border and no title


Dialog: Dialog class has border and title. An instance of the Dialog class cannot exist without an associated instance
of the Frame class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container for holding components. An
instance of the Panel class provides a container to which to add components.
Frame: A frame has title, border and menu bars. It can contain several components like buttons, text fields, scrollbars
etc. This is most widely used container while developing an application in AWT.

We can create a GUI using Frame in two ways:


1) By extending Frame class
2) By creating the instance of Frame class

Creating Frame by extending Frame class


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TestFrame extends JFrame implements ActionListener
{
JTextField tf1;
TestFrame()
{
setLayout(null);
JButton b1=new JButton("Click me");
b1.setBounds(50,50,100,40);
add(b1);
b1.addActionListener(this);

tf1=new JTextField();
tf1.setBounds(160,50,100,40);
add(tf1);
setSize(400,400);
setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{
tf1.setText("Hello India");
}
public static void main(String []st)
{
TestFrame tf=new TestFrame();
}
}
//setBounds(x,y,w,h)-Used to set the position and size of GUI component on the Frame

Creating Frame by creating instance of Frame class


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class TestFrame implements ActionListener


{
JFrame jf;
JTextField tf1;
TestFrame()
{

jf=new JFrame();
jf.setLayout(null);
JButton b1=new JButton("Click me");
b1.setBounds(50,50,100,40);
jf.add(b1);
b1.addActionListener(this);

tf1=new JTextField();
tf1.setBounds(160,50,100,40);
jf.add(tf1);
jf.setSize(400,400);
jf.setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{
tf1.setText("Hello India");
}
public static void main(String []st)
{
TestFrame tf=new TestFrame();
}
}
AWT controls:
- Adding Controls: add(CompObj), Removing Controls: remove(CompObj)
Label-> (Passive control do not support interaction with user)
Class:- Label(), Label(String), Label(String, How) //How:Label.LEFT/RIGHT/CENTER
Methods:setText(String), String getText(), setAlignment(HOW)
Push Button: (Used to generate event on click)
Class:Button(), Button(String)
Methods:setLabel(String), String getLabel()
Interface:ActionListener(Method:actionPerformed(ActionEvent ae)
ae.getSource()(For Name of control), ae.getctionCommand()(For label)
Check Box:(Used to turn ON or OFF any option)
Class: CheckBox(),CheckBox(String, boolean on), CheckBox(String, boolean on,ChkBxGroup)
Methods:Boolean getState(),setState(Boolean on), String GetLabel(),setLabel(string)
CheckBoxGroup class: (Used to select only one checkbox in group)
Methods:CheckBox getSelectctedChec kBox(), setSelectedCheckBox(CheckBox)
Choice List: (Used to create pop-up list to select one option, form of menu)
Class: Choice()
Methods: add(String)//to add list item, String getSelectedItem(), int getSelectedIndex(),
int getItemCount(),select(int index/String name), String getItem(int index)
List: (Used for compact, multi choice, scrolling selection list. Cam show & select multiple at once)
Class: List(),List(int numOfVisibleRows), List(int numOfVisibleRows, boolean multyiSelect)
Methos: add(String name),add(string name,int index), String getSelectedItem(), int getSelectedIndex()
String[] getSelectedItems(), int[] getSelectedIndexes(),int getItemCount(),select(int index),
String getItem(int index)
Scroll Bar: (Used to select continuous value between min & max limit)
Class:Scrollbar(),Scrollbar(int style)// style: Scrollbar.VERTICAL/HORIZONTAL
Scrollbar(int style, int initialVal,int thumbSize,int min,int max)
Methods:setValues(int initialVal,int thumbSize,int min,int max), int getValue(),int setValue(int),
int getMinimum(),int getMaximum(),setUnitIncrement(int),setBlockIncrement(int)
Interface:AdjustmentListener, method:getAdjustmentType(AdjustmentEvent AE)
Text Editing:
1. TextField(), TextField(int size), TextField(String), TextField(String, int size)
Methods: setText(String), String getText(),String getSelectedText(),select(int start, int end),
boolean isEditable(),setEditable(true/false),setEchoChar(Char)
2. TextArea()

You might also like