JavaFX不能拖放

时间:2019-09-26 15:56:57

标签: java javafx

我有这个界面:

@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();

}

我想激活TextField的拖放操作,当我将文件拖放到输入上方时,将显示文件的位置。

我的问题是我无法在界面中删除任何东西。

当我将文件拖到输入上方时,显示禁止标志:

enter image description here

当我尝试拖动文件时,整个界面上都会显示此标志。

1 个答案:

答案 0 :(得分:4)

您需要设置onDragOver属性以启用文件的复制/移动。像这样:

fileField.setOnDragOver((e) -> {
    if (e.getGestureSource() != fileField && e.getDragboard().hasFiles()) {
        e.acceptTransferModes(TransferMode.COPY_OR_MOVE);
    }
    e.consume();
});

此外,行fileField.setDisable(true);使您无法将文件拖放到TextField上。而是:

fileField.setEditable(false);

这将允许您拖放文件来设置文本,但不允许用户手动编辑文本。
完整的测试代码:

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();
    }
}

结果:
Text cannot be edited manually
掉落时:
Selected 3 folders and dropped into TextField