Action Listener in Java
By Dr. Jaspreet Singh Bajaj
By: Dr. Jaspreet S. Bajaj 1
Introduction to Action Listener
One of the most important components in AWT is the ActionListener interface.
It is a key element for adding interactivity in Java applications by handling user
actions. In this article, let us understand about the ActionListener interface in
detail.
In Java AWT, the ActionListener interface is a part of the 'java.awt.event'
package. When you click on a button, menu item, or a check box, the Java
ActionListener is called. It is notified in reference to an ActionEvent. It only has
one method, actionPerformed(). The principle of an ActionListener is to record
and respond to user interactions with GUI components.
By: Dr. Jaspreet S. Bajaj 2
Introduction to Action Listener
Configure the component with the Listener
component.addActionListener(new ActionListener() ); // Attaches an instance of
MyActionListener to the Component and registers an event handler.
public void actionPerformed(ActionEvent e){
//Write the code here
}
By: Dr. Jaspreet S. Bajaj 3
Contd…
In Java, the addActionListener method is defined in the following classes and
interfaces:
1. AbstractButton (Base class of JButton, JCheckBox and RadioButton)
2. JComboBox
3. JMenuItem,JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem
4. ActionListener (Interface)
By: Dr. Jaspreet S. Bajaj 4
ActionEvent
The ActionEvent class in Java is part of the Java AWT (Abstract Window Toolkit)
event handling mechanism. It is used to represent an event that occurs when a
user interacts with a GUI component, such as clicking a button, selecting a
menu item, or pressing the Enter key in a text field.
button.addActionListener(new ActionListener() );
public void actionPerformed(ActionEvent e){
//Write the code here
}
When the button is clicked, an ActionEvent is generated, and the
actionPerformed() method is triggered.
By: Dr. Jaspreet S. Bajaj 5
Contd…
When to Use ActionEvent?
When handling button clicks.
When responding to menu selections.
When detecting text field submissions (pressing Enter in a JTextField).
When processing custom action events in Java GUI applications.
By: Dr. Jaspreet S. Bajaj 6
Events in Java
An event can be defined as changing the state of an object or behavior by
performing actions. Actions can be a button click, cursor movement, keypress
through keyboard or page scrolling, etc.
The java.awt.event package can be used to provide various event classes.
Classification of Events
• Foreground Events
• Background Events
By: Dr. Jaspreet S. Bajaj 7
Contd…
By: Dr. Jaspreet S. Bajaj 8
1. Foreground Events
Foreground events are the events that require user interaction to generate, i.e.,
foreground events are generated due to interaction by the user on components
in Graphic User Interface (GUI). Interactions are nothing but clicking on a
button, scrolling the scroll bar, cursor moments, etc.
2. Background Events
Events that don’t require interactions of users to generate are known as
background events. Examples of these events are operating system
failures/interrupts, operation completion, etc.
By: Dr. Jaspreet S. Bajaj 9
Event Handling in Java
It is a mechanism to control the events and to decide what should happen after
an event occur. To handle the events, Java follows the Delegation Event model.
Delegation Event model
• It has Sources and Listeners.
By: Dr. Jaspreet S. Bajaj 10
Source: Events are generated from the source. There are various sources like
buttons, checkboxes, list, menu-item, choice, scrollbar, text components,
windows, etc., to generate events.
Listeners: Listeners are used for handling the events generated from the source.
Each of these listeners represents interfaces that are responsible for handling
events.
By: Dr. Jaspreet S. Bajaj 11
Registering the Source With Listener
Different Classes provide different registration methods.
Syntax:
addTypeListener()
where Type represents the type of event.
Example 1: For KeyEvent we use
addKeyListener() to register.
Example 2:that For ActionEvent we use
addActionListener() to register.
By: Dr. Jaspreet S. Bajaj 12
Event Classes in Java
Event Class Listener Interface Description
An event that indicates that a
component-defined action occurred like a
ActionEvent ActionListener
button click or selecting an item from the
menu-item list.
The adjustment event is emitted by an
AdjustmentEvent AdjustmentListener
Adjustable object like Scrollbar.
An event that indicates that a component
ComponentEvent ComponentListener moved, the size changed or changed its
visibility.
When a component is added to a
ContainerEvent ContainerListener container (or) removed from it, then this
event is generated by a container object.
These are focus-related events, which
FocusEvent FocusListener
include focus, focusin, focusout, and blur.
By: Dr. Jaspreet S. Bajaj 13
Event Name Listener Interface Description
An event that indicates whether an item
ItemEvent ItemListener
was selected or not.
An event that occurs due to a sequence of
KeyEvent KeyListener
keypresses on the keyboard.
The events that occur due to the user
MouseEvent MouseListener & MouseMotionListener interaction with the mouse (Pointing
Device).
An event that specifies that the mouse
MouseWheelEvent MouseWheelListener
wheel was rotated in a component.
An event that occurs when an object’s
TextEvent TextListener
text changes.
An event which indicates whether a
WindowEvent WindowListener
window has changed its status
By: Dr. Jaspreet S. Bajaj 14
Note: As Interfaces contains abstract methods which need to implemented by
the registered class to handle events.
By: Dr. Jaspreet S. Bajaj 15
Listener Interface Methods
ActionListener •actionPerformed()
AdjustmentListener •adjustmentValueChanged()
•componentResized()
ComponentListener •componentShown()
•componentMoved()
•componentHidden()
ContainerListener •componentAdded()
•componentRemoved()
FocusListener •focusGained()
•focusLost()
ItemListener •itemStateChanged()
By: Dr. Jaspreet S. Bajaj 16
•keyTyped()
KeyListener •keyPressed()
•keyReleased()
•mousePressed()
•mouseClicked()
MouseListener •mouseEntered()
•mouseExited()
•mouseReleased()
MouseMotionListener •mouseMoved()
•mouseDragged()
MouseWheelListener •mouseWheelMoved()
TextListener •textChanged()
•windowActivated()
•windowDeactivated()
•windowOpened()
WindowListener •windowClosed()
•windowClosing()
•windowIconified()
•windowDeiconified()
By: Dr. Jaspreet S. Bajaj 17
Flow of Event Handling
1. User Interaction with a component is required to generate an event.
2. The object of the respective event class is created automatically after event generation, and it
holds all information of the event source.
3. The newly created object is passed to the methods of the registered listener.
4. The method executes and returns the result.
By: Dr. Jaspreet S. Bajaj 18
Java JOptionPane
In Java, JOptionPane is a part of the Java Swing library. It helps us to create
dialog boxes such as message dialogs, conformation dialogs, input dialogs, and
options dialogs In this article, we are going to explore some constructors,
methods, and some examples of JOptionPane.
By: Dr. Jaspreet S. Bajaj 19
Constructors of JOptionPane Class
Constructors Description
JOptionPane() This is the default constructor for JOptionPane.It is used to
create a JOptionPane with no options and message.
JOptionPane(Object message)
It creates a message dialog with a specified message.
JOptionPane(Object message, int
It creates a message dialog with a specified message and its
messageType)
Type.
JOptionPane(Object message, int It helps us to create a dialog with a specified message,
messageType, int optionType) message type, and option type.
By: Dr. Jaspreet S. Bajaj 20
Methods of JOptionPane
Methods Description
It helps us to create JDialog with a specified title but without any
createDialog(String title)
parent.
showMessageDialog(Component
This method displays a message dialog with the specified message.
parentComponent, Object message)
showInputDialog(Component
This method displays an input dialog with the specified message.
parentComponent, Object message)
setMessageType(int messageType) This method to set the message type of the dialog
setOptionType(int optionType) This method allows you to set the option type for the dialog.
setOptions(Object[] options) Here we can set a list of custom options.
This method sets the initial selection when using custom options in
setInitialValue(Object initialValue)
the dialog.
By: Dr. Jaspreet S. Bajaj 21
Fields of JOptionPane
Fields Description
int ERROR_MESSAGE A Constant code displaying an error message icon.
int INFORMATION_MESSAGE A Constant code displaying information message icon.
int WARNING_MESSAGE A Constant code for displaying Warning message icon.
int YES_NO_OPTION A constant for creating a dialog with "Yes" and "No" options.
A constant for creating a dialog with "Yes," "No," and "Cancel"
int YES_NO_CANCEL_OPTION options.
A constant for creating a dialog with "OK" and "Cancel"
int OK_CANCEL_OPTION options.
By: Dr. Jaspreet S. Bajaj 22
Write the java program to demonstrate the various JComponents and
fetch the values of each and every component and display on the
dialog box
By: Dr. Jaspreet S. Bajaj 23
JScrollBar
Java's Swing library provides various GUI components for creating graphical user
interfaces. Among these components, is the JScrollBar class, which allows users
to scroll through the content of a component, such as a JScrollPane. In Java
JScrollBar is a part of javax.swing package.
By: Dr. Jaspreet S. Bajaj 24
JScrollPane
Java JScrollPane is a component in the Java Swing library that provides a
scrollable view of another component, usually a JPanel or a JTextArea. it
provides a scrolling functionality to the display for which the size changes
dynamically. It is useful to display the content which exceeds the visible area of
the window. In this article, we are going to see some constructors, methods,
and examples of JScrollPane.
By: Dr. Jaspreet S. Bajaj 25
JScrollBar vs JScrollPane
By: Dr. Jaspreet S. Bajaj 26
Java JTable
The JTable class is a part of Java Swing Package and is generally used to display
or edit two-dimensional data that is having both rows and columns. It is similar
to a spreadsheet. This arranges data in a tabular form.
By: Dr. Jaspreet S. Bajaj 27
Java JTree
The JTree is a type of GUI(Graphic User Interface) that displays information in a
hierarchical way. This intricate component part provides a quite elegant
substance of representing relationships among elements in a tree-like structure.
By: Dr. Jaspreet S. Bajaj 28
By: Dr. Jaspreet S. Bajaj 29