我已经在Windows JVM 7_25中测试了此代码,它运行良好。
import java.util.Random;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class DraggableFlowPane extends Application {
@Override
public void start(Stage primaryStage) {
final FlowPane root = new FlowPane();
final Random rng = new Random();
final int NUM_NODES = 120;
for (int i = 0; i < NUM_NODES; i++) {
int red = rng.nextInt(256);
int green = rng.nextInt(256);
int blue = rng.nextInt(256);
Node node = createNode();
node.setStyle(String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue));
root.getChildren().add(node);
}
primaryStage.setScene(new Scene(root, 600, 500));
primaryStage.show();
}
private Node createNode() {
final BorderPane bp = new BorderPane();
bp.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboard = new ClipboardContent();
final int nodeIndex = bp.getParent().getChildrenUnmodifiable()
.indexOf(bp);
clipboard.putString(Integer.toString(nodeIndex));
db.setContent(clipboard);
event.consume();
}
});
bp.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
boolean accept = true;
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString()) {
int incomingIndex = Integer.parseInt(dragboard.getString());
int myIndex = bp.getParent().getChildrenUnmodifiable()
.indexOf(bp);
if (incomingIndex == myIndex) {
accept = false;
}
} else {
accept = false;
}
if (accept) {
event.acceptTransferModes(TransferMode.MOVE);
}
}
});
bp.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
boolean success = false;
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString()) {
int incomingIndex = Integer.parseInt(dragboard.getString());
final Pane parent = (Pane) bp.getParent();
final ObservableList<Node> children = parent.getChildren();
int myIndex = children.indexOf(bp);
final int laterIndex = Math.max(incomingIndex, myIndex);
Node removedLater = children.remove(laterIndex);
final int earlierIndex = Math.min(incomingIndex, myIndex);
Node removedEarlier = children.remove(earlierIndex);
children.add(earlierIndex, removedLater);
children.add(laterIndex, removedEarlier);
success = true;
}
event.setDropCompleted(success);
}
});
bp.setMinSize(50, 50);
return bp;
}
public static void main(String[] args) {
launch(args);
}
}
当我在具有JVM 7_25的Linux上测试相同的代码时,出现以下错误堆栈:
Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run652386450/DX-57DC.jar using platform /opt/jdk1.7.0_25/bin/java
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragEnter(Scene.java:2523)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$1.run(GlassSceneDnDEventHandler.java:63)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$1.run(GlassSceneDnDEventHandler.java:54)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragEnter(GlassSceneDnDEventHandler.java:54)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragEnter(GlassViewEventHandler.java:395)
at com.sun.glass.ui.View.handleDragEnter(View.java:692)
at com.sun.glass.ui.View.notifyDragEnter(View.java:1005)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)
at com.sun.glass.ui.View.handleDragOver(View.java:701)
at com.sun.glass.ui.View.notifyDragOver(View.java:1010)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)
at com.sun.glass.ui.View.handleDragOver(View.java:701)
at com.sun.glass.ui.View.notifyDragOver(View.java:1010)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)
at com.sun.glass.ui.View.handleDragOver(View.java:701)
at com.sun.glass.ui.View.notifyDragOver(View.java:1010)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)
at com.sun.glass.ui.View.handleDragOver(View.java:701)
at com.sun.glass.ui.View.notifyDragOver(View.java:1010)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)
at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)
at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)
at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)
at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)
at com.sun.glass.ui.View.handleDragOver(View.java:701)
at com.sun.glass.ui.View.notifyDragOver(View.java:1010)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)
at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
................................................................
(和更多)
int incomingIndex = Integer.parseInt(dragboard.getString());
的内容为Null您能告诉我如何解决此问题或重写代码吗?
最佳答案
检查您dragboard.hasString()
。如果FALSE
为null,则绝对应返回dragboard.getString()
。因此,您永远不会到达int incomingIndex = Integer.parseInt(dragboard.getString());
,因为您不会通过低谷if (dragboard.hasString())
如果找不到问题,请粘贴Dragboard
类实现。
编辑:
好吧,这将更加强大:
private Node createNode() {
final BorderPane bp = new BorderPane();
bp.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboard = new ClipboardContent();
final int nodeIndex = bp.getParent().getChildrenUnmodifiable()
.indexOf(bp);
clipboard.putString(Integer.toString(nodeIndex));
db.setContent(clipboard);
event.consume();
}
});
bp.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
boolean accept = true;
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString()) {
try {
int incomingIndex = Integer.parseInt(dragboard.getString());
int myIndex = bp.getParent().getChildrenUnmodifiable()
.indexOf(bp);
if (incomingIndex == myIndex) {
accept = false;
}
} catch(java.lang.NumberFormatException e) {
//handle null or not number string in clipboard
accept = false;
}
} else {
accept = false;
}
if (accept) {
event.acceptTransferModes(TransferMode.MOVE);
}
}
});
bp.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
boolean success = false;
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString()) {
try {
int incomingIndex = Integer.parseInt(dragboard.getString());
final Pane parent = (Pane) bp.getParent();
final ObservableList<Node> children = parent.getChildren();
int myIndex = children.indexOf(bp);
final int laterIndex = Math.max(incomingIndex, myIndex);
Node removedLater = children.remove(laterIndex);
final int earlierIndex = Math.min(incomingIndex, myIndex);
Node removedEarlier = children.remove(earlierIndex);
children.add(earlierIndex, removedLater);
children.add(laterIndex, removedEarlier);
success = true;
} catch(java.lang.NumberFormatException e) {
//handle null or not number string in clipboard
}
}
event.setDropCompleted(success);
}
});
bp.setMinSize(50, 50);
return bp;
}