2. INTRODUCTION
Abstract Window Toolkit (AWT) is an API to develop GUI or Window based
applications in Java.
A Graphical User Interface (GUI) is built of graphical elements called
component. AWT provides various components such as buttons, labels, text fields,
checkboxes etc. used for creating GUI elements for Java applications.
Java AWT components are platform dependent that is components are
displayed according to the view of operating system.
The Container class is a subclass of component. Container class used to
display non-container class. AWT provides containers like Panels, Frames and Dialogues
to organize and group components in the application.
In Java GUI can be design using same predefined classes. All these classes
are defined in java.awt package.
4. Container Class
Window: The Window is a top-level container that represents a graphical
window or dialog box. Window class extends the container class,
which means it can contain other components such as buttons, labels
and text fields.
Frame: The Frame is the container that contains title bar and border and can
menu bars. It can have other components like button, text field etc.
Panel: The Panel is the container that doesn’t contain title bar and menu bars,
It is a light weight container that can be used grouping other components
together within a window or a frame. It can have other components like
button, text field etc.
Dialog: It represents a rectangular area where application can draw something
or can receive inputs created by user.
Note: Before adding the components that make up a user interface, the programmer must create a container.
5. Creating the Frame
Create a subclass of Frame
Override any of the standard window methods, such as init(), start(), and paint()
Implement the windowClosing() method of the windowlistener interface.
Calling setVisible(false) when the window is closed.
Once you have defined a Frame subclass, you can create an object of that class.
But it will note be initially visible when created, the window is given a default height
and width.
You can set the size of the window explicity by calling the following method
Void setSize(int height, int width);
Void setSize(dimension d);
dimension d = new dimension(400, 400);
6. Frame Methods
setTitle(String)
setBackground(color)
setForground(color)
setSize(dimension)
setVisible(boolean)
setLayout()
Add(component)
- used to set display user defined message on title bar.
- used to set background color.
- used to set foreground or text color.
- set the width and height for frame.
- Set the frame is visible or not.
- Used to set any layout to the frame.
- Add component to the frame.
7. Event Handling
Event :
An event is an Object that describes a
state change in a Source.
Some of the activities that cause event
to be generated are:
• Pressing a Button.
• Entering a character through Key
Board.
• Selecting an item from a list etc.
Event Source :
A source is an object that generates an
event. Some general Event sources
are,
Buttons
CheckBox
List
Text items
8. EVENT HANDLING
Here is a general form for adding a listener to an event source :
public void addTypeListner(TypeEvent e)
Here,
type is the name of the event.
e is the reference of the event listener.
Event Listener
A Listener is an object that is notified when an event occurs.
For example:
mouseMotionaListner interface define two events:
When mouse is dragged.
When mouse is moved.
For implementing event listener we have to import the following statements:
import java.awt.event.*;
9. HOW TO USE EVENTS
Components defined in the AWT generate AWTEvents.
Identify which events you wish to listen for (some components generate more
than one type of event)
Identify the Listener class which Listens for the event
Once we have identified the Listener interface, implement the Listener
interface.
Our listener class must register with the component to received events
Call the addXXXListener method where XXX is the Event type.
10. AWT EVENT
ActionEvent - Action has occurred (e.g. Button pressed)
AdjustmentEvent - “Adjustable” Component changed
ComponentEvent - Component Changed
ContainerEvent - Container changed (add or remove)
FocusEvent - Focus Changed
ItemEvent - Item selected or deselected
MouseEvent - Mouse event
KeyEvent - Keyboard event
TextEvent - Text changed evets
WindowEvent - Window related events
The following is a list of events in the java.awt.event package:
12. ActionEvent
The ActionEvent is generated when button is clicked or the item of a list is double
clicked.
To write an Action Listener, follow the steps given below:
First declare an event handler class and specify that the class either implements an
ActionListner interface or extends a class that implements an ActionListener
interface.
For example
Public class MyClass implements ActionListener {
//abstract methods of ActionListner interface
}
13. ActionEvent Listener
Register an instance of the event handler class as a listener on one or more
components.
For example:
someComponent.addActionListner(instanceOfMyClass)
Include code that implements the methods in listener interface.
Public void actionPerformed(ActionEvent e){
If(e.getSource()==someComponent(){
//code that reacts to the action
}
}
14. KeyEvent
On entering the character from any source generates the key event. This interface
has 3 methods. These are
Public void keyEvent(keyEvent ae){
//active on typing a code .....
}
Public void keyPressed(KeyEvent ae){
//active on pressing a key…….
}
Public void keyPressed(KeyEvent ae){
//active on pressing a key .....
}
15. Mouse Event
• This event indicates a mouse action occurred in a component.
• This class has 5 interface method as follows:
Public void mousePressed(MouseEvent e) {…….}
Called just after the user presses a mouse button while the cursor is over the
listened-to component.
Public void mouseReleased(MouseEvent e) {…….}
Called just after the user releases a mouse button after a mouse press over the
listened-to component
Public void mouseClicked(MouseEvent e) {……..}
Called just after the user clicks the listened-to component.
Public void mouseEntered(MouseEvent e) {……}
Called just after the cursor enters the bounds of the listened-to component.
Public void mouseExcited(MouseEvent e) {…….}
Called just after the cursor exits the bounds of the listened-to component.
16. MouseMotionEvent
• The interface MouseMotionEvent indicates a mouse action occurred in a
component.
• This low level event is generated by a component object when mouse is
dragged or moved.
• This class provides 2 interface methods:
mouseDragged Method:
executed when mouse is dragged over the listened-to component..
mouseMoved Method:
execute when mouse is moved over the listened-to component...
17. FocusEvent
This class provide two interface methods:
focusGained:
Called just after the listened-to component gets the focus.
focusLost:
Called just after the listened-to component Loses the focus.
18. WindowEvent Listener
This class provide 8 interface methods:
windowOpened:
Called just after the listened-to window has been shown for the first time.
windowClosing:
Called in response to a user request for the listened-to window to be closed. To
actually close the window, the listener should invoke the windowsdispose or
setVisible(false) method.
windowClosed:
Called just after the listened-to window has closed.
windowIconified:
Called just after the listened-to window is iconified.
windowDeiconified:
Called just after the listened-to window is deiconified.
19. WindowEvent Class
• windowActivated and windowDeactivated:
Called just after the listened-to window is activated or deactivated,
respectively. These methods are not send to windows that are not frames
or dialogs.
We use windowGainedFocus and windowLostFocus methods to
determine when a window gains or loses the focus.
20. ItemEvent Class
This class provide one interface method:
itemStateChanged:
Called just after a state change in the listened-to component.