Reputation: 3225
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.
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.
Upvotes: 1
Views: 119
Reputation: 2641
You need to set the onDragOver
property to enable copying/moving of the file. 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);
disables you from being able to drop files on your 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();
}
}
Upvotes: 4