[英]JavaFX cannot drag and drop
I have this interface:我有这个界面:
@Override
public void start(Stage primaryStage){
Text fileText = new Text("File:");
TextField fileField = new TextField();
fileField.setDisable(true);
fileField.setMinWidth(250);
fileField.setOnDragDropped(e -> {
Dragboard db = e.getDragboard();
boolean success = false;
if (db.hasFiles()){
fileField.setText(db.getFiles().toString());
success = true;
}
e.setDropCompleted(success);
e.consume();
});
Button clearButton = new Button("Clear");
clearButton.setOnMouseClicked(e -> fileField.setText(""));
HBox hboxFile = new HBox();
hboxFile.setAlignment(Pos.CENTER_LEFT);
hboxFile.getChildren().addAll(fileText, fileField, clearButton);
HBox.setMargin(fileText, new Insets(10));
HBox.setMargin(fileField, new Insets(10, 10, 10, 0));
HBox.setMargin(clearButton, new Insets(10, 10, 10, 0));
primaryStage.setScene(new Scene(hboxFile));
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
primaryStage.show();
}
I want to activate drag and drop for TextField
, when I drop an file over input, will appear location of file.我想为TextField
激活拖放,当我将文件放在输入上时,将出现文件的位置。
My problem is I cannot drop anything in interface.我的问题是我不能在界面中删除任何东西。
When I drag file over input appear interdict sign:当我将文件拖到输入上时出现禁止标志:
This sign is present all over interface when I try to drag a file.当我尝试拖动文件时,这个标志出现在整个界面上。
You need to set the onDragOver
property to enable copying/moving of the file.您需要设置onDragOver
属性以启用文件的复制/移动。 Like so:像这样:
fileField.setOnDragOver((e) -> {
if (e.getGestureSource() != fileField && e.getDragboard().hasFiles()) {
e.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
e.consume();
});
Furthermore, the line fileField.setDisable(true);
此外,行fileField.setDisable(true);
disables you from being able to drop files on your TextField.使您无法在 TextField 上放置文件。 Instead, do:相反,请执行以下操作:
fileField.setEditable(false);
This will allow you to drag/drop files to set the text, but it won't let a user edit the text manually.这将允许您拖放文件来设置文本,但它不会让用户手动编辑文本。
The complete testing code:完整的测试代码:
public class TestApp extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) {
Text fileText = new Text("File:");
TextField fileField = new TextField();
fileField.setMinWidth(250);
fileField.setPromptText("Drop file here");
fileField.setOnDragOver((e) -> {
if (e.getGestureSource() != fileField && e.getDragboard().hasFiles()) {
e.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
e.consume();
});
fileField.setEditable(false);
fileField.setOnDragDropped(e -> {
Dragboard db = e.getDragboard();
boolean success = false;
if (db.hasFiles()) {
fileField.setText(db.getFiles().toString());
success = true;
}
e.setDropCompleted(success);
e.consume();
});
Button clearButton = new Button("Clear");
clearButton.setOnMouseClicked(e -> fileField.setText(""));
HBox hboxFile = new HBox();
hboxFile.setAlignment(Pos.CENTER_LEFT);
hboxFile.getChildren().addAll(fileText, fileField, clearButton);
HBox.setMargin(fileText, new Insets(10));
HBox.setMargin(fileField, new Insets(10, 10, 10, 0));
HBox.setMargin(clearButton, new Insets(10, 10, 10, 0));
primaryStage.setScene(new Scene(hboxFile));
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
primaryStage.show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.