19 Java AWT
19 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.
AWT hierarchy
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.
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
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()