英文:
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:
<?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="Drag .jar file here" />
</children>
</AnchorPane>
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 -> {
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()) {
// Process the dropped file
System.out.println("Dropped file: " + 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\<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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论