3

我在 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

你能告诉我如何解决这个问题或重写代码吗?

4

3 回答 3

5

看来您null作为 int 传递,并且null无法转换为Integer(或 int)

int incomingIndex = Integer.parseInt(dragboard.getString());  

检查 的输出dragboard.getString()

于 2013-08-04T13:56:36.120 回答
2

彼得,

我正在通过此链接了解有关拖放功能的更多信息。- http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm。从他们在那里给出的例子来看,你似乎已经完成了预期的事情。

来到您的实际代码:我会尝试了解我是否将 null 放入 setOnDragDetected({}) 中的 ClipboardContent 对象;ClipboardContent 类扩展了一个 HashMap - http://docs.oracle.com/javafx/2/api/javafx/scene/input/ClipboardContent.html

如果您尝试获取的 nodeIndex 不是 DataFormat.PLAIN_TEXT,则 clipboard.putString() 可能实际上并未将索引放入哈希图中。请参阅语句 - “将此值设置为 null 会有效地将其从剪贴板中清除。” 在 javadoc 中并确认这是否没有发生。

如果“put”操作成功,putString 方法返回一个布尔值作为 True。

我会打印这两件事('putString()' 的输出和'nodeIndex' 的值)来调查出了什么问题。

-问候,拉维。

于 2013-08-16T19:48:54.587 回答
1

检查你dragboard.hasString()。绝对它应该在is nullFALSE的情况下返回。所以,你永远达不到,因为你不会通过低谷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;  
    } 
于 2013-08-13T16:36:02.527 回答