Course Content‎ > ‎Session 7‎ > ‎

Listener/Adapter Types

Lets look at the Listener/Adapter types for different types of events:


This listener interface does not have an adapter class and has only one method, which needs to be implemented to listen to the action event, which is given below:

    void actionPerformed(ActionEvent)
    Here are some of the examples when ActionEvent is generated:
        - User clicks a button
        - User presses return, after typing in a text field
        - User chooses an item from the menu.


It is the listener interface for receiving item events. It has the following method, which needs to be invoked, when an item selection occurs:

    void itemStateChanged(ItemEvent)


It has an adapter class with the following methods:

    void mouseClicked(MouseEvent)    
    void mouseEntered(MouseEvent)
    void mouseExited(MouseEvent)
    void mousePressed(MouseEvent)
    void mouseReleased(MouseEvent)

    MouseEvent is generated when:
        - the user presses a mouse button
        - the cursor is over a Component.


This interface also has an adapter class called MouseMotionAdapter. It is implemented when the user moves the mouse over a Component. Its methods are:

    void mouseDragged(MouseEvent)
    void mouseMoved(MouseEvent)

It is implemented when a window is opened, closed, iconified or deiconified, activated or deactivated. It has an adapter class called WindowAdapter. Following methods are included with this interface:

    void windowDeactivated(WindowEvent e)
    void windowClosed(WindowEvent e)   
    void windowIconified(WindowEvent e)
    void windowOpened(WindowEvent e)
    void windowClosing(WindowEvent e)
    void windowDeiconified(WindowEvent e)
    void windowActivated(WindowEvent e)


This interface is called, when a component loses or gains keyboard focus. Its methods include:

    void focusLost(FocusEvent)
    void focusGained(FocusEvent)


This interface is called, when one or more component events are generated by a Component object, just after the component is hidden, moved, resized or made visible. Its methods include:

    void componentHidden(ComponentEvent)
    void componentMoved(ComponentEvent)
    void componentResized(ComponentEvent)
    void componentShown(ComponentEvent)  

Lets look at an example, to understand the concept of listeners. In the example below, an ActionListener is implemented.

    //Program showing ActionListener Event
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class compEvents extends JFrame implements ActionListener
        private JButton boldButton;
        private JButton italicsButton;
        private JButton regularButton;
        private JTextField textField;
        public compEvents()
            Container contentPane = getContentPane();
            contentPane.setLayout(new GridLayout(0, 1));
            boldButton = new JButton("Bold Arial Font");
            italicsButton = new JButton("Italics Arial Font");
            regularButton = new JButton("Regular Arial Font");
            textField = new JTextField("Click on button to see the type of     
            addWindowListener(new WindowAdapter()
                public void windowClosing(WindowEvent e)
            } );
        public void actionPerformed(ActionEvent evt)
            Object source = evt.getSource();
            Font boldFont = new Font("Arial", Font.BOLD, 12);
            Font italicsFont = new Font("Arial", Font.ITALIC, 12);
            Font regularFont = new Font("Arial", Font.PLAIN, 12);
            if (source == boldButton)
                textField.setText("Bold Arial Font, Size 12");
            else if (source == italicsButton)
                textField.setText("Italics Arial Font, Size 12");
            else if (source == regularButton)
                textField.setText("Regular Arial Font, Size 12");
        public static void main(String[] args)
            compEvents window = new compEvents();
            window.setTitle("Components and Events");


Analysis of the Program

In the above example in compEvents class, four components are used  three buttons (boldButton, italicsButton, regularButton) and a text field (textField). textField by default shows this text  Click on button to see the type of font . Text alignment in the textField is changed to center alignment, by calling setHorizontalAlignment() method, which takes a static field CENTER. CompEvents() constructor is called to create an instance of all these components. Content pane is created by calling the getContentPane() method. In this example, we are using GridLayout with one column and as many rows as we need. Then, each component registers itself to listen to the actionEvent, by calling addListener method and passing itself as the reference (as in this line boldButton.addActionListener(this)). actionPerformed method takes an ActionEvent evt. Depending on which button is pressed, it changes the text and font of the text field. If button showing Bold Arial Font is pressed, text field changes the text to Bold Arial Font, Size 12 with the appropriate font type and size.