关于JavaFX应用程序中.jar文件的拖放功能存在问题。

huangapple go评论63阅读模式
英文:

Issue with drag and drop functionality for .jar files in JavaFX application

问题

JavaFX 应用程序中拖放功能存在问题

我目前正在开发一个 JavaFX 应用程序,并尝试实现拖放功能。在我的用户界面中,我有一个面板,我希望用户能够拖放文件,特别是 .jar 文件。但是,当我将文件拖动到面板上时,出现了一个取消符号的光标,并且拖放事件未触发。

我尝试过各种方法,包括为面板设置 onDragEntered、onDragExited、onDragOver 和 onDragDropped 事件处理程序。我还将 TransferMode 设置为 COPY 并验证了 dragboard 是否有文件。然而,期望的行为并没有按预期工作。

以下是我的 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.detector.Controller">
    <stylesheets>
        <URL value="@styles.css" />
    </stylesheets>
    <children>
        <Pane fx:id="dragDropPane" layoutY="-3.0" prefHeight="349.0" prefWidth="600.0" />
        <Button layoutX="545.0" layoutY="361.0" onAction="#onOpenButtonClick" text="Open" />
        <TextField fx:id="pathText" layoutX="14.0" layoutY="361.0" prefHeight="25.0" prefWidth="515.0" />
      <Label layoutX="255.0" layoutY="192.0" text="拖放 .jar 文件至此" />
    </children>
</AnchorPane>

以下是我的控制器代码:

import javafx.fxml.FXML;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;

import java.io.File;

public class Controller {
    @FXML
    public Pane dragDropPane;

    @FXML
    public void initialize() {
        dragDropPane.setOnDragEntered(event -> {
            if (event.getGestureSource() != dragDropPane && event.getDragboard().hasFiles()) {
                dragDropPane.setStyle("-fx-border-color: blue; -fx-border-width: 2;");
                event.acceptTransferModes(TransferMode.COPY);
            }
            event.consume();
        });

        dragDropPane.setOnDragExited(event -> {
            dragDropPane.setStyle("-fx-border-color: transparent;");
            event.consume();
        });

        dragDropPane.setOnDragOver(event -> {
            if (event.getGestureSource() != dragDropPane && event.getDragboard().hasFiles()) {
                event.acceptTransferModes(TransferMode.COPY);
            }
            event.consume();
        });

        dragDropPane.setOnDragDropped(event -> {
            Dragboard dragboard = event.getDragboard();
            boolean success = false;
            if (dragboard.hasFiles()) {
                for (File file : dragboard.getFiles()) {
                    // 处理拖放的文件
                    System.out.println("拖放的文件: " + file.getAbsolutePath());
                }
                success = true;
            }
            event.setDropCompleted(success);
            event.consume();
        });
    }
}

我不确定为什么拖放功能未按预期工作。感谢任何关于如何解决此问题的见解或建议。谢谢!

英文:

Issue with drag and drop functionality in JavaFX application

I'm currently working on a JavaFX application and trying to implement a drag and drop functionality. I have a pane in my UI where I want users to be able to drag and drop files, specifically .jar files. However, I'm encountering an issue where the cursor shows a cancel sign when I drag a file over the pane, and the drop event is not triggered.

I've tried various approaches, including setting the onDragEntered, onDragExited, onDragOver, and onDragDropped event handlers for the pane. I've also set the TransferMode to COPY and verified that the dragboard has files. However, the desired behavior is not working as expected.

Here is my FXML file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import java.net.*?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/17.0.2-ea&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;com.example.detector.Controller&quot;&gt;
&lt;stylesheets&gt;
&lt;URL value=&quot;@styles.css&quot; /&gt;
&lt;/stylesheets&gt;
&lt;children&gt;
&lt;Pane fx:id=&quot;dragDropPane&quot; layoutY=&quot;-3.0&quot; prefHeight=&quot;349.0&quot; prefWidth=&quot;600.0&quot; /&gt;
&lt;Button layoutX=&quot;545.0&quot; layoutY=&quot;361.0&quot; onAction=&quot;#onOpenButtonClick&quot; text=&quot;Open&quot; /&gt;
&lt;TextField fx:id=&quot;pathText&quot; layoutX=&quot;14.0&quot; layoutY=&quot;361.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;515.0&quot; /&gt;
&lt;Label layoutX=&quot;255.0&quot; layoutY=&quot;192.0&quot; text=&quot;Drag .jar file here&quot; /&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;

And here is my controller code:

import javafx.fxml.FXML;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import java.io.File;
public class Controller {
@FXML
public Pane dragDropPane;
@FXML
public void initialize() {
dragDropPane.setOnDragEntered(event -&gt; {
if (event.getGestureSource() != dragDropPane &amp;&amp; event.getDragboard().hasFiles()) {
dragDropPane.setStyle(&quot;-fx-border-color: blue; -fx-border-width: 2;&quot;);
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dragDropPane.setOnDragExited(event -&gt; {
dragDropPane.setStyle(&quot;-fx-border-color: transparent;&quot;);
event.consume();
});
dragDropPane.setOnDragOver(event -&gt; {
if (event.getGestureSource() != dragDropPane &amp;&amp; event.getDragboard().hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dragDropPane.setOnDragDropped(event -&gt; {
Dragboard dragboard = event.getDragboard();
boolean success = false;
if (dragboard.hasFiles()) {
for (File file : dragboard.getFiles()) {
// Process the dropped file
System.out.println(&quot;Dropped file: &quot; + file.getAbsolutePath());
}
success = true;
}
event.setDropCompleted(success);
event.consume();
});
}
}

I'm unsure why the drag and drop functionality is not working as expected. I appreciate any insights or suggestions on how to resolve this issue. Thank you!

答案1

得分: 3

I copied the initialize method contents into the start method of a new app and added the bits that would allow it to display and run and it worked for me with no issue. (JavaFX & JDK 20 on Win 11).

我将初始化方法的内容复制到了新应用程序的start方法中,并添加了允许其显示和运行的部分,对我来说没有任何问题。 (JavaFX和JDK 20在Win 11上)。

I dropped files from the file explorer into the app window and they were named in the system out, as per the drop handler code.

我从文件资源管理器中将文件拖放到应用程序窗口中,它们的名称会在系统输出中显示,与拖放处理代码相符。

Output

输出

javafx.scene.input.DragEvent[source=Pane@6b2cc6e[styleClass=root]]
Dropped file: C:\Users<username>\OneDrive\Documents\flight.txt

Code

代码

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.File;

public class CopyHandler extends Application {

public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Pane dropPane = new Pane();
dropPane.setPrefSize(600, 400);
dropPane.setOnDragEntered(event -> {
System.out.println(event);
if (event.getGestureSource() != dropPane && event.getDragboard().hasFiles()) {
dropPane.setStyle("-fx-border-color: blue; -fx-border-width: 2;");
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dropPane.setOnDragExited(event -> {
dropPane.setStyle("-fx-border-color: transparent;");
event.consume();
});
dropPane.setOnDragOver(event -> {
if (event.getGestureSource() != dropPane && event.getDragboard().hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dropPane.setOnDragDropped(event -> {
Dragboard dragboard = event.getDragboard();
boolean success = false;
if (dragboard.hasFiles()) {
for (File file : dragboard.getFiles()) {
// Process the dropped file
System.out.println("Dropped file: " + file.getAbsolutePath());
}
success = true;
}
event.setDropCompleted(success);
event.consume();
});
primaryStage.setScene(new Scene(dropPane));
primaryStage.show();
}

}

英文:

I copied the initialize method contents into the start method of a new app and added the bits that would allow it to display and run and it worked for me with no issue. (JavaFX & JDK 20 on Win 11).

I dropped files from the file explorer into the app window and they were named in the system out, as per the drop handler code.

Output

javafx.scene.input.DragEvent[source=Pane@6b2cc6e[styleClass=root]]
Dropped file: C:\Users\&lt;username&gt;\OneDrive\Documents\flight.txt

Code

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.File;
public class CopyHandler extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Pane dropPane = new Pane();
dropPane.setPrefSize(600, 400);
dropPane.setOnDragEntered(event -&gt; {
System.out.println(event);
if (event.getGestureSource() != dropPane &amp;&amp; event.getDragboard().hasFiles()) {
dropPane.setStyle(&quot;-fx-border-color: blue; -fx-border-width: 2;&quot;);
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dropPane.setOnDragExited(event -&gt; {
dropPane.setStyle(&quot;-fx-border-color: transparent;&quot;);
event.consume();
});
dropPane.setOnDragOver(event -&gt; {
if (event.getGestureSource() != dropPane &amp;&amp; event.getDragboard().hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
});
dropPane.setOnDragDropped(event -&gt; {
Dragboard dragboard = event.getDragboard();
boolean success = false;
if (dragboard.hasFiles()) {
for (File file : dragboard.getFiles()) {
// Process the dropped file
System.out.println(&quot;Dropped file: &quot; + file.getAbsolutePath());
}
success = true;
}
event.setDropCompleted(success);
event.consume();
});
primaryStage.setScene(new Scene(dropPane));
primaryStage.show();
}
}

huangapple
  • 本文由 发表于 2023年5月21日 06:34:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定