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.
Â
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:
Â
Â
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.
Â
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.
Â
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:
Â
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
Following are the three steps to add ActionListener Interface
Step 1: Implement the ActionListener Interface in the class.
Syntax:
Step 2: Now Register all the components with the Listener.
Syntax:
Step 3: Aylast override the actionPerformed() method.
Syntax:
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.
Â
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:
Â
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:
Â
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:
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.
Â
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.
Â
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.
In the console it prints the following messages like: onlineexamguide
==> opened onlineexamguide
==> activated onlineexamguide
==> closing onlineexamguide
==> deactivated onlineexamguide
==> closed