java - double click on jlist in netbeans -
i want select item in list mouse. found code didn't work.
mouselistener mouselistener = new mouseadapter() { public void mouseclicked(mouseevent e) { if (e.getclickcount() == 2) { int index = list.locationtoindex(e.getpoint()); system.out.println("double clicked on item " + index); } } }; list.addmouselistener(mouselistener);
try code, sample application works fine !!!!!!
import java.awt.borderlayout; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jscrollpane; public class calc { public static void main(string args[]) { string labels[] = { "a", "b", "c", "d", "e", "f", "g", "h" }; jframe frame = new jframe("selecting jlist"); frame.setdefaultcloseoperation(jframe.exit_on_close); jlist jlist = new jlist(labels); jscrollpane scrollpane1 = new jscrollpane(jlist); frame.add(scrollpane1, borderlayout.center); mouselistener mouselistener = new mouseadapter() { public void mouseclicked(mouseevent mouseevent) { jlist thelist = (jlist) mouseevent.getsource(); if (mouseevent.getclickcount() == 2) { int index = thelist.locationtoindex(mouseevent.getpoint()); if (index >= 0) { object o = thelist.getmodel().getelementat(index); system.out.println("double-clicked on: " + o.tostring()); } } } }; jlist.addmouselistener(mouselistener); frame.setsize(350, 200); frame.setvisible(true); } }
Comments
Post a Comment