Java Abstract Window Toolkit(AWT)
In our previous topic, we discussed some components of AWT API, now here we will discuss rest of the components and event listeners.
AWT MenuItem and Menu
In Java, AWT contains a MenuItem and Menu Class.MenuItem is used for adding Lable in Menu. The menu is used to create a drop-down of menu components
MenuItem declaration
public class MenuItem extends MenuComponent implements Accessible
Menu declaration
public class Menu extends MenuItem implements MenuContainer, Accessible
Example:
In this example, we are creating a menu item that contains sub menu as well. We use MenuItem and Menu class for creating menu.
import java.awt.*;
class MenuDemo1
{
MenuDemo1()
{
Frame menu_f= new Frame("studytonight ==> Menu and MenuItem Demo");
MenuBarmenu_bar=new MenuBar();
Menu menu11=new Menu("Menu");
Menu sub_menu1=new Menu("Sub Menu =>");
MenuItem a1=new MenuItem("Red");
MenuItem a2=new MenuItem("Light Red");
MenuItem a3=new MenuItem("Drak Red");
MenuItem b1=new MenuItem("Green");
MenuItem b2=new MenuItem("Light Green");
MenuItem b3=new MenuItem("Dark Green");
menu11.add(a1);
sub_menu1.add(a2);
sub_menu1.add(a3);
menu11.add(b1);
sub_menu1.add(b2);
sub_menu1.add(b3);
menu11.add(sub_menu1);
menu_bar.add(menu11);
menu_f.setMenuBar(menu_bar);
menu_f.setSize(400,400);
menu_f.setLayout(null);
menu_f.setVisible(true);
}
public static void main(String args[])
{
new MenuDemo1();
}
}
AWT PopupMenu
In Java, AWT contains a Popup Menu. It is a popup which is dynamic in nature.
PopupMenu declaration
public class PopupMenu extends Menu implements MenuContainer, Accessible
Example:
import java.awt.*;
import java.awt.event.*;
class PopupMenuDemo1
{
PopupMenuDemo1()
{
final Frame pop_menuf= new Frame("studytonight ==>PopupMenu Demo");
final PopupMenupop_menu = new PopupMenu("*Edit*");
MenuItempop_cut = new MenuItem("Cut");
pop_cut.setActionCommand("Cut");
MenuItempop_copy = new MenuItem("Copy");
pop_copy.setActionCommand("Copy");
MenuItempop_paste = new MenuItem("Paste");
pop_paste.setActionCommand("Paste");
pop_menu.add(pop_cut);
pop_menu.add(pop_copy);
pop_menu.add(pop_paste);
pop_menuf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent a)
{
pop_menu.show(pop_menuf , a.getX(), a.getY());
}
});
pop_menuf.add(pop_menu);
pop_menuf.setSize(400,400);
pop_menuf.setLayout(null);
pop_menuf.setVisible(true);
}
public static void main(String args[])
{
new PopupMenuDemo1();
}
}
AWT Panel
In Java, AWT contains a Panel. The panel provides a free space where components can be placed.
Panel declaration
public class Panel extends Container implements Accessible
Example:
Lets create a panel to add components like: button, textbox etc. the panel provides a place to add awt components.
import java.awt.*;
public class PanelDemo1{
PanelDemo1()
{
Frame panel_f= new Frame("studytonight ==> Panel Demo");
Panel panel11=new Panel();
panel11.setBounds(40,80,200,200);
panel11.setBackground(Color.red);
Button box1=new Button("On");
box1.setBounds(50,100,80,30);
box1.setBackground(Color.gray);
Button box2=new Button("Off");
box2.setBounds(100,100,80,30);
box2.setBackground(Color.gray);
panel11.add(box1);
panel11.add(box2);
panel_f.add(panel11);
panel_f.setSize(400,400);
panel_f.setLayout(null);
panel_f.setVisible(true);
}
public static void main(String args[])
{
new PanelDemo1();
}
}
AWT Dialog
In Java, AWT contains a Dialog. It is a type of window which is having a border and a title. But it does not have any maximize and minimize button.
Declaration
public class Dialog extends Window
Example:
In this example, we are creating a dialogue box. The dialogue box is used to provide information to the user.
import java.awt.*;
import java.awt.event.*;
public class DialogDemo1
{
private static Dialog dialog_d;
DialogDemo1()
{
Frame dialog_f= new Frame();
dialog_d = new Dialog(dialog_f , "studytonight ==> Dialog Demo", true);
dialog_d.setLayout( new FlowLayout() );
Button dialog_b = new Button ("OK");
dialog_b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogDemo1.dialog_d.setVisible(false);
}
});
dialog_d.add( new Label ("Welcome to studytonight. Click on button to continue."));
dialog_d.add(dialog_b);
dialog_d.setSize(300,300);
dialog_d.setVisible(true);
}
public static void main(String args[])
{
new DialogDemo1();
}
}
AWT Toolkit
In Java, AWT contains a Toolkit. It is a superclass of Abstract Window Toolkit and can be implemented anywhere.
Declaration
public abstract class Toolkit extends Object
Example:
import java.awt.*;
import java.awt.event.*;
public class ToolkitDemo1
{
public static void main(String[] args)
{
Frame toolkit_f=new Frame("studytonight ==> Toolkit Demo");
Button toolkit_b=new Button("beep");
toolkit_b.setBounds(50,100,60,30);
toolkit_f.add(toolkit_b);
toolkit_f.setSize(300,300);
toolkit_f.setLayout(null);
toolkit_f.setVisible(true);
toolkit_b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
Toolkit.getDefaultToolkit().beep();
}
});
}
}
ActionListener Interface
In java, ActionListener Interface is present under java.awt.event package. This interface is used when you want to notify click action on button or menu item. It has actionPeformed()
method.
Syntax
public abstract void actionPerformed(ActionEvent e)
Following are the three steps to add ActionListener Interface
Step 1: Implement the ActionListener Interface in the class.
Syntax:
public class ActionListenerDemo Implements ActionListener
Step 2: Now Register all the components with the Listener.
Syntax:
component.addActionListener(instanceOfListenerclass);
Step 3: Aylast override the actionPerformed() method.
Syntax:
public void actionPerformed(ActionEvent e)
{
//statements
}
Example:
We can use action listener to implement event in awt component. Event can be anything like: mouse clock, mouse dragged etc. in this example, we are implementing actionlistener.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ActionListenerDemo1 implements ActionListener {
JButton aL_button;
JFrame aL_frame;
JTextArea aL_textArea;
public ActionListenerDemo1() {
aL_button = new JButton("Click Me");
aL_frame = new JFrame("studytonight ==>ActionListener Demo");
aL_textArea = new JTextArea(50, 50);
aL_button.addActionListener(this);
aL_textArea.setLineWrap(true);
aL_frame.setLayout(new BorderLayout());
aL_frame.add(aL_textArea, BorderLayout.NORTH);
aL_frame.add(aL_button, BorderLayout.SOUTH);
aL_frame.pack();
aL_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aL_frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
aL_textArea.setText(aL_textArea.getText().concat("Welocme to studytonight.com\n"));
}
public static void main(String args[]) {
ActionListenerDemo1 obj= new ActionListenerDemo1();
}
}
MouseListener Interface
In Java, MouseListener Interface is under java.awt.event package.This interface is used when the state of the mouse is changed. It has 5 methods which are as following:
1. public abstract void mouseClicked(MouseEvent e);
2. public abstract void mouseEntered(MouseEvent e);
3. public abstract void mouseExited(MouseEvent e);
4. public abstract void mousePressed(MouseEvent e);
5. public abstract void mouseReleased(MouseEvent e);
Example:
import java.awt.*;
import java.awt.event.*;
public class MouseListenerDemo1 extends Frame implements MouseListener{
Label mL_l;
MouseListenerDemo1(){
addMouseListener(this);
mL_l=new Label();
mL_l.setBounds(10,20,500,100);
add(mL_l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Entered");
}
public void mouseExited(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Exited");
}
public void mousePressed(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Released");
}
public static void main(String[] args) {
new MouseListenerDemo1();
}
}
MouseMotionListener Interface
In Java, MouseMotionListener Interface is under java.awt.event package.This interface is used whenever the mouse is moved or dragged. It has 2 methods which are as following:
1. public abstract void mouseDragged(MouseEvent e);
2. public abstract void mouseMoved(MouseEvent e);
Example:
import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerDemo1 extends Frame implements MouseMotionListener{
MouseMotionListenerDemo1(){
addMouseMotionListener(this);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent a) {
Graphics mM_g=getGraphics();
mM_g.setColor(Color.ORANGE);
mM_g.fillOval(a.getX(),a.getY(),10,10);
}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args) {
new MouseMotionListenerDemo1();
}
}
ItemListener Interface
In Java, ItemListener Interface is under java.awt.event package. This interface is used whenever the checkbox is clicked. It has itemStateChanged() method.
Syntax:
public abstract void itemStateChanged(ItemEvent e)
Example:
This interface is used to handle item listener events like: item selected or checkbox checked. In this example, we are handling checkbox checked event.
import java.awt.*;
import java.awt.event.*;
public class ItemListenerDemo1 implements ItemListener{
Checkbox iT_checkBox1,iT_checkBox2;
Label iT_label;
ItemListenerDemo1(){
Frame iT_f= new Frame("studytonight ==>CheckBox Demo");
iT_label = new Label();
iT_label.setAlignment(Label.CENTER);
iT_label.setSize(400,100);
iT_checkBox1 = new Checkbox("Core Java");
iT_checkBox1.setBounds(100,100, 100,40);
iT_checkBox2 = new Checkbox("jsp");
iT_checkBox2.setBounds(100,150, 100,40);
iT_f.add(iT_checkBox1);
iT_f.add(iT_checkBox2);
iT_f.add(iT_label);
iT_checkBox1.addItemListener(this);
iT_checkBox2.addItemListener(this);
iT_f.setSize(400,400);
iT_f.setLayout(null);
iT_f.setVisible(true);
}
public void itemStateChanged(ItemEventiT) {
if(iT.getSource()==iT_checkBox1)
iT_label.setText("Core Java Checkbox: "+ (iT.getStateChange()==1?"checked":"unchecked"));
if(iT.getSource()==iT_checkBox2)
iT_label.setText("jsp Checkbox: "+ (iT.getStateChange()==1?"checked":"unchecked"));
}
public static void main(String args[])
{
new ItemListenerDemo1();
}
}
KeyListener Interface
In Java, KeyListener Interface is under java.awt.event package. This interface is used when the state of the key is changed. It has 3 methods which are as following:
1. public abstract void keyPressed(KeyEvent e);
2. public abstract void keyReleased(KeyEvent e);
3. public abstract void keyTyped(KeyEvent e);
Example:
In this example, we are using keylistener interface to handle key events that can be key release, typed etc. see the below example.
import java.awt.*;
import java.awt.event.*;
public class KeyListenerDemo1 extends Frame implements KeyListener{
Label kL_l;
TextArea kL_area;
KeyListenerDemo1(){
kL_l=new Label();
kL_l.setBounds(20,50,500,20);
kL_area=new TextArea();
kL_area.setBounds(20,80,300, 300);
kL_area.addKeyListener(this);
add(kL_l);
add(kL_area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
kL_l.setText("studytonight ==> Key Pressed");
}
public void keyReleased(KeyEvent e) {
kL_l.setText("studytonight ==> Key Released");
}
public void keyTyped(KeyEvent e) {
kL_l.setText("studytonight ==> Key Typed");
}
public static void main(String[] args) {
new KeyListenerDemo1();
}
}
WindowListener Interface
In Java, WindowListener Interface is under java.awt.event package. This interface is used when the state of the window is changed. It has 7 methods which are as following:
1. public abstract void windowActivated(WindowEvent e);
2. public abstract void windowClosed(WindowEvent e);
3. public abstract void windowClosing(WindowEvent e);
4. public abstract void windowDeactivated(WindowEvent e);
5. public abstract void windowDeiconified(WindowEvent e);
6. public abstract void windowIconified(WindowEvent e);
7. public abstract void windowOpened(WindowEvent e);
Example:
In this example, we are handling windows events like: window open, close etc.
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowDemo1 extends Frame implements WindowListener
{
WindowDemo1()
{
addWindowListener(this);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new WindowDemo1();
}
public void windowActivated(WindowEvent arg0)
{
System.out.println("studytonight ==> activated");
}
public void windowClosed(WindowEvent arg0)
{
System.out.println("studytonight ==> closed");
}
public void windowClosing(WindowEvent arg0)
{
System.out.println("studytonight ==> closing");
dispose();
}
public void windowDeactivated(WindowEvent arg0)
{
System.out.println("studytonight ==> deactivated");
}
public void windowDeiconified(WindowEvent arg0)
{
System.out.println("studytonight ==>deiconified");
}
public void windowIconified(WindowEvent arg0)
{
System.out.println("studytonight ==>iconified");
}
public void windowOpened(WindowEvent arg0)
{
System.out.println("studytonight ==> opened");
}
}
In the console it prints the following messages like:
studytonight ==> opened
studytonight ==> activated
studytonight ==> closing
studytonight ==> deactivated
studytonight ==> closed