Advance Desktop Applications Lecture 9
Advance Desktop Applications Lecture 9
Components Class: In java all the elements like the button, textfiels,
scrollbar, etc are called components. In Java AWT, there are classes for each
component as shown in the above diagram but we need to add them to a
container in order to place them in a particular position on the screen.
Container Class: The Container class is sub-class of Component class. The
classes that extend Container class are known as containers such as Frame,
Dialog and Panel. It also contains components like Button, TextField, Label
etc.
public void setSize(int width,int height) Set the width and height of component
ex. Frame
Label is a control which display text on the window. This text can not be
edited by the user.
The commonly used constructor for label class are:
•Label()
•Label(String text)
•Label(String text, int align)
THE COMMONLY USED METHOD OF LABEL
CLASS
Method Description
getText() Gets the text of this Label
setBound(int x, int y, int width, int To sets the position and size of
height) component
setForeground(Color.red) To set foreground color
Method Description
String getText() Return the text inputted in the text field
Ex. String txt=txt1.getText();
setBound(int x, int y, int width, int To sets the position and size of text field
height)
setEchoChar(Char) To change the text field to password field
Ex. txt1.setEchoChar(‘*’)
Buttons are the AWT controls which trigger some action when they are
clicked. Button are mainly used for handling events for example submitting
information to the database or validating information of a form.
The constructor used with button class are as follows:
•Button()
•Button(String)
import java.awt.Button;
Button button=new Button(“Click Me”);
button.setBounds(x,y,w,h) Y
frame.add(button);
X
H
W
CODE
package AWTpack; f.add(b);
import java.awt.*; f.setSize(400,400);
f.setLocation(600, 300);
public class ButtonEx3 { f.setLayout(null);
Frame f; f.setVisible(true);
Button b; }
ButtonEx3(){ public static void main(String args[])
f = new Frame("AWT Frame"); {
b = new Button("Click Here!"); ButtonEx3 obj=new ButtonEx3();
}
b.setBounds(50,100,80,30); }
b.setSize(130, 50);
b.setBackground(Color.black);
b.setForeground(Color.white);
b.setFont(new Font("Arial",Font.BOLD|
Font.ITALIC, 15));
Check boxes are used for making multiple selections to indicate optional
features in a form like hobbies. They have two states either on or off,
checked or unchecked, true or false.
The constructor used with Checkbox class are as follow:
•Checkbox()
•Checkbox(String)
•Checkbox(String ,boolean)
Ex. Checkbox chk=new Checkbox(“Label Text for Checkbox”, true);
add(chk);
Method Description
setState(boolean) Sets the state of a check box
Ex. Chk1.setState(“true”);
}
KABUL UNIVERSITY Monday, July 29, 2024 27
KABUL UNIVERSITY Monday, July 29, 2024 28
CHECKBOX GROUP
Checkbox class is also used to create radio button in a form. Radio button is
used for making mutually exclusive selections. It means only one option can
be selected from a set of options.
So, first we need to create object of ChexkboxGroup class and then pass it to
Checkbox as bellow:
CheckboxGroup chkg=new CheckboxGroup();
Checkbox chk=new Checkbox(String,chkg,boolean);
add(chk);
}}
KABUL UNIVERSITY Monday, July 29, 2024 30
KABUL UNIVERSITY Monday, July 29, 2024 31
CHOICE
The AWT control Choice is used to display a drop-down menu of items from
which only one can be selected at a time. It has only one constructor given
bellow:
Choice chList=new Choice();
chList.add(“Graduate”);
chList.add(“Post Graduate”);
chList.add(“PHD”);
Add(chList);
Method Description
add(String item) Add items to the Choice list
Ex. chList.add(“Graduate”);
The AWT List control is similar to Choice control with two difference. In list
control user can select more than one items and selected items is not
displayed on the top of the list.
The following constructors are available in List class:
•List()
•List(int)
•List(int,boolean) // here Boolean specify that multiple selection is allowed or
not it can be true or false