add Double Click Event to JList - Java Swing

Java examples for Swing:JList

Description

add Double Click Event to JList

Demo Code


//package com.java2s;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JList;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main {
    public static void addDoubleClickEvent(JList list) {
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JList source = (JList) e.getSource();
                if (e.getClickCount() == 2) {
                    ListSelectionListener[] listeners = source
                            .getListSelectionListeners();
                    for (int i = 0; i < listeners.length; i++) {
                        listeners[i].valueChanged(new ListSelectionEvent(
                                source, source.getSelectedIndex(), source
                                        .getSelectedIndex(), false));
                    }// ww w.j a  v  a 2 s  .c o m
                }
            }
        });
    }
}

Related Tutorials