AWT COMPONENTS
AWT COMPONENTS
• When we press a button and release it, AWT sends an instance of ActionEvent to that
button by calling processEvent on the button.
• The processEvent method of the button receives the all the events, then it passes an
action event by calling its own method processActionEvent. This method passes the
action event on to action listeners that are interested in the action events generated by
the button.
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.
1. Checkbox() It constructs a
checkbox with no string
as the label.
2. Checkbox(String label) It constructs a
checkbox with the
given label.
3. Checkbox(String label, It constructs a
boolean state) checkbox with the
given label and sets
the given state.
4. Checkbox(String label, It constructs a
boolean state, checkbox with the
CheckboxGroup group) given label, set the
given state in the
specified checkbox
group.
// importing AWT class
import java.awt.*;
public class CheckboxExample1
{
// constructor to initialize
CheckboxExample1() {
// creating the frame with the title
Frame f = new Frame("Checkbox Example");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
// setting location of checkbox in frame
checkbox2.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
f.add(checkbox1);
f.add(checkbox2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} // main method
public static void main (String args[])
{
new CheckboxExample1();
}
}
Java AWT CheckboxGroup
The object of CheckboxGroup class is used to group together a set of Checkbox.
At a time only one check box button is allowed to be in "on" state and remaining
check box button in "off" state. It inherits the object class.
// main method
public static void main(String args[])
{
new ListExample1();
}
}
Choice Class constructor
// class constructor
ChoiceExample1() {
// creating a frame
Frame f = new Frame();
// main method
public static void main(String args[])
{
new ChoiceExample1();
}
}
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.
It can be added to top-level container like Frame or a component like
Panel. The Scrollbar class extends the Component class.
AWT Scrollbar Class Declaration
public class Scrollbar extends Component implements Adjustable, A
ccessible
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Java BoxLayout
The Java BoxLayout class is used to arrange the components either vertically or
horizontally. For this purpose, the BoxLayout class provides four constants. They are
as follows:
public BoxLayoutExample1 () {
buttons = new Button [5];
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver space
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e) {
card.next(c);
}