How can we catch a double click and enter key events for a JList in Java?

A JList can extend JComponent class that allows the user to choose either a single or multiple selections. A JList can generate a ListSelectiionListener interface and it includes one abstract method valueChanged(). A JList can also generate a MouseListener interface to catch a double click event in the list and generates a KeyListener interface to catch an enter key event.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class JListTest extends JFrame {
   public JListTest() {
      setTitle("JList Test");
      setLayout(new FlowLayout());
      Vector v = new Vector();
      for (int i = 1; i < 15; i++) {
         v.addElement("Item #" + i);
      add(new JLabel("Double-clicked on: "));
      final JTextField dblTextField = new JTextField(15);
      add(new JLabel("Enter key on: "));
      final JTextField entTextField = new JTextField(15);
      final JList list = new JList(v);
      // catch double-click events
      list.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if (me.getClickCount() == 2) {
      // catch enter-key events
      list.addKeyListener(new KeyAdapter() {
         public void keyReleased(KeyEvent ke) {
            if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
      add(new JScrollPane(list));
      setSize(375, 250);
   public static void main(String[] args) {
      new JListTest();