Using ActionListener for AWT List

The following illustrates use of ActionListener for AWT List.

import java.awt.*;
import java.awt.event.*;
class ListAction extends Frame implements ActionListener
{
List l;

    public ListAction()
    {
        createAndShowGUI();
    }
  
    private void createAndShowGUI()
    {
        setTitle("List Action");
        setLayout(new FlowLayout());
       
        // Create and add items to list
        l=new List();
       
        l.add("Google");
        l.add("Yahoo!");
        l.add("Bing");
        l.add("Baidu");
       
        // Add list to frame
        add(l);
       
        // Add ActionListener
        l.addActionListener(this);
       
        setSize(400,400);
        setVisible(true);
    }
  
    public void actionPerformed(ActionEvent ae)
    {
        // Change the title
        setTitle("You double clicked "+l.getSelectedItem());
    }
  
    public static void main(String args[])
    {
        new ListAction();
    }
}

ListAction(): Code illustrating use of ActionListener with AWT List is written here.
Note: ActionEvent is generated when you double click on a list item.
You might also want to see using ItemListener for AWT List

Using ActionListener for AWT List


Posted by at on

Tags: AWT Event Handling,