Listeners
Listeners
Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.
Registration Methods
For registering the component with the Listener, many classes provide the registration methods. For
example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
static int SCROLLBARS_BOTH - It creates and displays both horizontal and vertical
scrollbars.
static int SCROLLBARS_HORIZONTAL_ONLY - It creates and displays only the
horizontal scrollbar.
static int SCROLLBARS_VERTICAL_ONLY - It creates and displays only the vertical
scrollbar.
static int SCROLLBARS_NONE - It doesn't create or display any scrollbar in the text
area.
Class constructors:
Sr.
Constructor Description
no.
1. TextArea() It constructs a new and empty text area with no text in it.
It constructs a new text area with specified number of rows
2. TextArea (int row, int column)
and columns and empty string as text.
It constructs a new text area and displays the specified text
3. TextArea (String text)
in it.
TextArea (String text, int row, It constructs a new text area with the specified text in the
4.
int column) text area and specified number of rows and columns.
TextArea (String text, int row, It construcst a new text area with specified text in text area
5.
int column, int scrollbars) and specified number of rows and columns and visibility.
java.awt.Component
java.lang.Object
java.awt.Component
java.lang.Object
java.awt.Component
java.lang.Object
Class methods
Sr.
Method name Description
no.
1. void addNotify() It creates the canvas's peer.
void createBufferStrategy (int It creates a new multi buffering strategies on the
2.
numBuffers) particular component.
It creates a new multi buffering strategies on the
void createBufferStrategy (int
3. particular component with the given buffer
numBuffers, BufferCapabilities caps)
capabilities.
AccessibleContext
4. It gets the accessible context related to the Canvas.
getAccessibleContext()
It returns the buffer strategy used by the particular
5. BufferStrategy getBufferStrategy()
component.
6. void paint(Graphics g) It paints the canvas with given Graphics object.
7. void pdate(Graphics g) It updates the canvas with given Graphics object.
actionPerformed() method
The actionPerformed() method is invoked automatically whenever you click on the registered
component.
1. public abstract void actionPerformed(ActionEvent e);
How to write ActionListener
The common approach is to implement the ActionListener. If you implement the ActionListener
class, you need to follow 3 steps:
1) Implement the ActionListener interface in the class: