AWT notes
AWT notes
AWT notes
The AWT tutorial will help the user to understand Java GUI programming in
simple and easy steps.
3. Platform-Independent Libraries:
The Libraries of AWT are written in Java which they are totally
platform-independent. Because of this, it ensures that AWT
functionality remains consistent across different environments.
Java AWT Hierarchy
Components: AWT provides various components such
as buttons, labels, text fields, checkboxes, etc used for
creating GUI elements for Java Applications.
Containers: AWT provides containers like panels,
frames, and dialogues to organize and group components
in the Application.
Layout Managers: Layout Managers are responsible for
arranging data in the containers some of the layout
managers are BorderLayout, FlowLayout, etc.
Event Handling: AWT allows the user to handle the
events like mouse clicks, key presses, etc. using event
listeners and adapters.
Graphics and Drawing: It is the feature of AWT that
helps to draw shapes, insert images and write text in the
components of a Java Application.
Note: Container can be added inside another container as it is
type of component.
9. AWT Scrollbar
Syntax of AWT Scrollbar:
public class Scrollbar extends Component implements Adjustabl
e, Accessible
Java AWT Scrollbar Class Constructors
There are three constructor classes in Java mentioned below:
1.Scrollbar():
It Creates a new vertical Scrollbar in the Application.
2.Scrollbar(int-orientation):
Creates a new vertical Scrollbar with the given orientation.
3. Scrollbar(int orientation, int value, int visible, int mini,
int maxi):
Creates a new scrollbar with the orientation mentioned with
value as the default value and [mini, maxi] as the lower and
higher limit.
10. Java AWT MenuItem & Menu
MenuItem class adds a simple labeled menu item on the menu.
The MenuItem class allows you to create individual items that
can be added to menus. And Menu is a component used to
create a dropdown menu that can contain a list of MenuItem
components.
Syntax of Java AWT MenuItem
public class MenuItem extends MenuComponent implements Access
ible
Syntax of Java AWT Menu
public class Menu extends MenuItem implements MenuContainer,
Accessible
Java AWT PopupMenu is a component that is used for
dynamically popping up a menu that appears when the user
right-clicks or performs any other action on a component.
Syntax of AWT PopupMenu
public class PopupMenu extends Menu implements MenuContainer,
Accessible
import java.awt.*;
b.addActionListener((e)->tf.setText("Clicked "+count++));
f.setLayout(new FlowLayout());
f.setSize(500,500);
f.setVisible(true);
}}
public FirstApp()
{
super("My App");
setLayout(new FlowLayout());
l=new Label("Name");
tf=new TextField(20);
b=new Button("OK");
add(l);
add(tf);
add(b);
}
public static void main(String[] args)
{
FirstApp f=new FirstApp();
f.setSize(400,400);
f.setVisible(true);
}
}
2. Java MouseListener
Java MouseListner is a interface that responds to the actions
performed by mouse events generated by the user. Example:
mouse clicks , mouse movements, etc.
There are 5 Methods associated with MouseListner:
1. mouseClicked(MouseEvent e):
Responds to mouse buttons when clicked on a component in the
Application.
2. mousePressed(MouseEvent e):
Responds to mouse button is Pressed on a component in the
Application.
3. mouseReleased(MouseEvent e):
Responds to Mouse button released after being pressed over a
component in the Application.
4. mouseEntered(MouseEvent e):
Responds to the situation when a Mouse cursor enters the
bounds of a component in an Application.
5. mouseExited(MouseEvent e):
Responds to the situation when a Mouse cursor exits a
component’s bounds.
3. Java MouseMotionListener
Java MouseMotionListner is a interface which is notified when
mouse is moved or dragged.
It contains two Methods mentioned below:
1. mouseDragged(MouseEvent e):
Responds when the mouse is dragged with mouse button clicked
over a component in Application.
2. mouseMoved(MouseEvent e):
Responds when the mouse is moved over a component in
Application.
4. Java ItemListener
Java ItemListner is an interface which handles events related to
item selection and deselection those that occur with checkboxes,
radio buttons, etc. There is only one Method associated with
ItemListner that is itemStateChanged(). This method provides
information about the event, i.e. source of the event and the
changed state.
Syntax of itemStateChanged() method:
itemStateChanged(ItemEvent e)
5. Java KeyListener
Java KeyListner is an interface in Java notified whenever you
change the state of key or can be said for key related events.
Syntax of KeyListener:
public interface KeyListener extends EventListener
There are three methods associated with KeyListner as
mentioned below:
1. keyPressed (KeyEvent e):
Responds to the event when key is pressed.
2. keyReleased (KeyEvent e):
Responds to the event when the key is released.
3. keyTyped (KeyEvent e):
Responds to the key has been typed.
6. Java WindowListener
Java WindowListener is a interface used for handling events
related to window actions. Events like opening , closing,
minimizing, etc are handled using WindowListener.
Syntax of WindowListener
public interface WindowListener extends EventListener
There are seven methods associated with WindowListener as
mentioned below:
1. windowActivated (WindowEvent e):
Responds when window is first opened
2. windowClosed (WindowEvent e):
Responds when the user attempts to close the window
3. windowClosing (WindowEvent e):
Responds after a window has been closed
4. windowDeactivated (WindowEvent e):
Responds when a window is minimized
5. windowDeiconified (WindowEvent e):
Responds when a window is restored from a minimized state
6. windowIconified (WindowEvent e):
Responds when a window is activated
7. windowOpened (WindowEvent e):
Responds when a window loses focus
In this section, we will discuss event processing and how to implement the
delegation event model in Java. We will also discuss the different
components of an Event Model.
Event Processing in Java
Java support event processing since Java 1.0. It provides support for AWT (
Abstract Window Toolkit), which is an API used to develop the Desktop
application. In Java 1.0, the AWT was based on inheritance. To catch and
process GUI events for a program, it should hold subclass GUI components
and override action() or handleEvent() methods. The below image
demonstrates the event processing.
But, the modern approach for event processing is based on the Delegation
Model. It defines a standard and compatible mechanism to generate and
process events. In this model, a source generates an event and forwards it
to one or more listeners. The listener waits until it receives an event. Once
it receives the event, it is processed by the listener and returns it. The UI
elements are able to delegate the processing of an event to a separate
function.
The key advantage of the Delegation Event Model is that the application
logic is completely separated from the interface logic.
In this model, the listener must be connected with a source to receive the
event notifications. Thus, the events will only be received by the listeners
who wish to receive them. So, this approach is more convenient than the
inheritance-based event model (in Java 1.0).
In the older model, an event was propagated up the containment until a
component was handled. This needed components to receive events that
were not processed, and it took lots of time. The Delegation Event model
overcame this issue.
o Events
o Events Sources
o Events Listeners
Events
The Events are the objects that define state change in a source. An event
can be generated as a reaction of a user while interacting with GUI
elements. Some of the event generation activities are moving the mouse
pointer, clicking on a button, pressing the keyboard key, selecting an item
from the list, and so on. We can also consider many other user operations
as events.
Event Sources
A source is an object that causes and generates an event. It generates an
event when the internal state of the object is changed. The sources are
allowed to generate several different types of events.
From the above syntax, the Type is the name of the event, and e1 is a
reference to the event listener. For example, for a keyboard event
listener, the method will be called as addKeyListener(). For the mouse
event listener, the method will be called as addMouseMotionListener().
When an event is triggered using the respected source, all the events will
be notified to registered listeners and receive the event object. This
process is known as event multicasting. In few cases, the event
notification will only be sent to listeners that register to receive them.
From the above syntax, the Type is an event name, and e2 is the
reference of the listener. For example, to remove the keyboard listener,
the removeKeyListener() method will be called.
The source provides the methods to add or remove listeners that generate
the events. For example, the Component class contains the methods to
operate on the different types of events, such as adding or removing them
from the listener.
Event Listeners
An event listener is an object that is invoked when an event triggers. The
listeners require two things; first, it must be registered with a source;
however, it can be registered with several resources to receive notification
about the events. Second, it must implement the methods to receive and
process the received notifications.
The methods that deal with the events are defined in a set of interfaces.
These interfaces can be found in the java.awt.event package.
Types of Events
The events are categories into the following two categories:
The foreground events are those events that require direct interaction of
the user. These types of events are generated as a result of user
interaction with the GUI component. For example, clicking on a button,
mouse movement, pressing a keyboard key, selecting an option from the
list, etc.
Design Goals
The design goals of the event delegation model are as following:
TestApp.java:
1. import java.awt.*;
2. import java.awt.event.*;
3.
4. public class TestApp {
5. public void search() {
6. // For searching
7. System.out.println("Searching...");
8. }
9. public void sort() {
10. // for sorting
11. System.out.println("Sorting....");
12. }
13.
14. static public void main(String args[]) {
15. TestApp app = new TestApp();
16. GUI gui = new GUI(app);
17. }
18.}
19.
20.class Command implements ActionListener {
21. static final int SEARCH = 0;
22. static final int SORT = 1;
23. int id;
24. TestApp app;
25.
26. public Command(int id, TestApp app) {
27. this.id = id;
28. this.app = app;
29. }
30.
31. public void actionPerformed(ActionEvent e) {
32. switch(id) {
33. case SEARCH:
34. app.search();
35. break;
36. case SORT:
37. app.sort();
38. break;
39. }
40. }
41. }
42.
43. class GUI {
44.
45. public GUI(TestApp app) {
46. Frame f = new Frame();
47. f.setLayout(new FlowLayout());
48.
49. Command searchCmd = new Command(Command.SEAR
CH, app);
50. Command sortCmd = new Command(Command.SORT, app);
51.
52. Button b;
53. f.add(b = new Button("Search"));
54. b.addActionListener(searchCmd);
55. f.add(b = new Button("Sort"));
56. b.addActionListener(sortCmd);
57.
58. List l;
59. f.add(l = new List());
60. l.add("Alphabetical");
61. l.add("Chronological");
62. l.addActionListener(sortCmd);
63. f.pack();
64.
65. f.show();
66. }
67. }