get Double Clicked Item In JList - Java Swing

Java examples for Swing:JList

Description

get Double Clicked Item In JList

Demo Code


//package com.java2s;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JList;

public class Main {
    public static Object getDoubleClickedItemInList(MouseEvent e) {
        if (!(e.getSource() instanceof JList)) // assert the source is of type JList
            return null;

        JList<?> list = (JList<?>) e.getSource();
        if (e.getClickCount() == 2) { // if double-click
            Rectangle r = list.getCellBounds(0, list.getLastVisibleIndex()); // bound the list
            if (r != null && r.contains(e.getPoint())) { // forces mouse to be over a part
                return list.getSelectedValue();
            }//  w w  w  .  jav  a 2 s. c  o  m
        }

        return null;
    }
}

Related Tutorials