java - Handling drag-and-drop with a moving list -


I am working with drag and drop to drag the user from somewhere to a list. However, when ListSelectionEvent is received, it can unexpectedly display a drag and drop when the user changes the selection.

My code:

  Import java.awt. *; Import javax.swing *; Class main {public static zero createGUI () {JFrame f = new JFrame (); JList & LT; String & gt; List = New JLIT & lt; & Gt; (New string [] {"text a", "text b"}); List.setFont (list.getFont () deriveFont (24f).); List.setDragEnabled (true); // list.setTransferHandler (new transfer handler ()) {/ * ... * /}); List.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); List.addListSelectListener (E-> f.setLocation (f.getX (), f.getY () + f.getHeight ())); List.setSelectedIndex (0); F.add (list, border layout .a); F.pack (); F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); F.setLocationByPlatform (true); F.setVisible (true); } Public static zero main (string [] args) {SwingUtilities.invokeLater (main :: createGUI); }}  

To regenerate, launch this application, place it above some applications that accept a drop (eg eclipses, notepad ++) and Toggles selection multiple times

I am using Windows 7 and JDK 1.8.0_5.

I tried but I did not find any work. How can I fix this issue? [Not really related] This is my real application: (with green cross icon)

< / P>

OK I think I understand your problem: you want DND enabled, not just when The user is changing his selection. You should try this (in java 7, with lambda bhav I'm not very comfortable, so I'm still not using Java 8. It will work on Java 8):

  class main {change the personal stable boolean list = false; Public static zero createGUI () {last JFrame f = new JFrame (); JList & LT; String & gt; List = new jellist & lt; String & gt; (New string [] {"text a", "text b"}); List.setFont (list.getFont () deriveFont (24f).); List.setDragEnabled (true); List.setTransferHandler (new transfer handler) {Private constant last long serialversionUID = 1L; @ Override Public Entouration Assessment (JCI) {If (List Changing) {listChanging = false; Return noone;} else {return COPY;}} @ override @SuspressWarnings ("uncheck") public transfersable versus transformable (jackcount C) {new string selection returns (JLIST & lt; string & gt; c) .getSelectedValue ());}}); List.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); List.addListSelectionListener (New list selection list) {@Override changed the public zero value (selection from list to event E) {f.setLocation (f.getX (), f.getY () + f.getHeight ()); List changed = true;}}); List.setSelectedIndex (0); F.add (list, border layout .a); F.pack (); F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); F.setLocationByPlatform (true); F.setVisible (true); } Public static zero main (string [] args) {SwingUtilities.invokeLater (New Runnabal) {@Override Public runs zero () {Main.createGUI ();}}); }}  

What is this code that when a user changes the user selection, it sets a variable listChanging to true is. When the user drag (with accident or purpose), it checks that if change the list is true , which means that it was probably an unexpected drag if there is no change in the list If it has occurred, then it allows the COPY drag.

Basically, if the drag was in a list change, then it disables DND. If the list has not changed, and the user has been manually dragged, then it enables DND.

Hope this satisfies all your needs :)


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -