java - How does one prevent a JList from selecting elements when dragging beyond the end of its list? -


a similar question has been asked here, , while acknowledge it, question's solution doesn't quite solve question. jlist, when clicked on, select index list item closest mouse click. jlist every click+drag event fire.

i prevent jlist selecting items during click+drag events when click+drag location outside visible list. how go this?

i had considered overriding different method, 1 involved in click+drag events of selecting list items. thought try setselectioninterval() method.

jlist<string list = new jlist<string>(){         private static final long serialversionuid = 1l;         @override         public int locationtoindex(point location) {             int index = super.locationtoindex(location);             if (index != -1 && !getcellbounds(index, index).contains(location)) {                 clearselection();                 return -1;                 //an excellent click-only solution prohibit selecting of                  //items beyond visible list             }             else {                 return index;             }         }         @override         public void setselectioninterval(int anchor, int lead) {             super.setselectioninterval(anchor, lead);             system.out.println("setselectioninterval");         }                }; 

i found each time click+drag anywhere on displayed jlist, system.out message of "setselectioninterval" added method above. don't know go here in terms of overriding methods. maybe not how should approach this. in source code setselectioninterval() got lost trying find way through whatever listener involved, came here. :p

i'd appreciate pointers should looking or solution altogether. in advance!

this sscce example close how set up. is, list not select item when click-only event fired away list items themselves. same effect happen when click+drag events fired away list items.

import java.awt.borderlayout; import java.awt.point;  import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jpanel;    public class testmain {      public static void main(string[] args) {         jframe frame = new jframe();         jpanel content = new jpanel(new borderlayout());          string[] data = {"luke", "jeff", "bryce"};         jlist<string> list = new jlist<string>(data){             private static final long serialversionuid = 1l;              @override             public int locationtoindex(point location) {                 system.out.println("location index");                 int index = super.locationtoindex(location);                 if (index != -1 && !getcellbounds(index, index).contains(location)) {                     clearselection();                     return -1;                 }                 else {                     return index;                 }             }         }          content.add(list, borderlayout.center);         frame.setcontentpane(content);         frame.setsize(200,200);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     }  } 

if disabling whole dragging feature acceptable, following should help:

@override protected void processmousemotionevent(mouseevent e) {     if(mouseevent.mouse_dragged != e.getid()) {         super.processmousemotionevent(e);     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -