FXFileChooser icon indicating copy to clipboard operation
FXFileChooser copied to clipboard

Allow to configure window modality

Open Oliver-Loeffler opened this issue 5 years ago • 3 comments

Currently FXFileChooser is always application modal. There are cases where different modality is needed.

Oliver-Loeffler avatar Feb 23 '20 19:02 Oliver-Loeffler

Hello, Thank you for this product. I would like to open a FXFileChooser in non modal manner like JFoenix JFXDialog. Intention is to use it in JPro where the UI thread cannot be blocked.

Does this solving this issue achieve it?

Hugolarson avatar May 19 '20 04:05 Hugolarson

Hello, well there will be a new version latest within next week, where modality will be configurable. Currently the default is Modality.APPLICATION_MODAL. Modality does not block the UI thread, it configures if underlaying windows (e.g. the main application window) are focusable and selectable. For case of Modality.APPLICATION_MODAL, the parent window of the dialog is not accessible as long the FXFileChooserStage is visible. After closing the FXFileChooserStage, the parent window again becomes accessible.

If in JPro any errors occurr due to JavaFX thread blocking, this is a bug and I would have to dig into.

To configure modality I plan to either modify the FXFileChooserStage.create() method or create some kind of a builder, a fluent API.

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("Show File Chooser");
        FXFileChooserStage fc = FXFileChooserStage.create(Skin.DARK);

        /* assume default Modality.WINDOW or Modality.NONE or request custom cas */
        FXFileChooserStage fc = FXFileChooserStage.create(Skin.DARK)
                                                  .modality( ... );

        // or
        FXFileChooserStage fc = FXFileChooserStage.create(Skin.DARK, Modality.NONE);

        button.setOnAction(evt-> fc.showOpenDialog(primaryStage).ifPresent(this::showSelection));

        Scene scene = new Scene(button);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Demo");
        primaryStage.show();
    }

If the stage created by FXFleChooser causes trouble in JPro, I don't know yet how to solve it. I will try out with JPro.

Which Java and JavaFX version do you run? Have you already tried to run FXFleChooser in JPro?

Running in JPro the FXFileChooser will show files on the server - which, you probably dont want to do. This can pose a security threat to the server. But, it depends on your use case. FXFileChooser running in browser will not be able to show the clients file system. This kind of access is blocked by all web browsers for security reasons.

Oliver-Loeffler avatar May 20 '20 23:05 Oliver-Loeffler

Hello, I use Java 8 and have not actually tried with JPro and for some reason thought FXFileChooser is always Modal. And yes FXFileChooser will not allow me to browse files on client I have learned. Thank you for the explanation.

Hugolarson avatar May 21 '20 12:05 Hugolarson

Configuration of modality for FXFileChooserStage is already working. This can be achieved by calling Stage.initModality(...) method like:

PathFilter[] filters = new PathFilter[] {all, xml, xlsx, na0, txt, exe, combined};
FXFileChooserStage fileChooserStage = FXFileChooserStage.create(Skin.DARK, local, filters);
fileChooserStage.initModality(Modality.NONE);

Button button = new Button("FXFileChooserStage (Skin.DARK, Modality.NONE)");
button.setOnAction(e -> {
        if (fileChooserStage.isShowing()) {
            e.consume();  // JavaFX will otherwise complain if stage is already showing
            return;
        }
        Optional<Path> selection = fcDarkNonModal.showOpenDialog(primaryStage);
        // handle selection here
    });

Alternatively one can make use of the class FileChooser which can be embedded as a control like a button into any other view or even in FXML. Modality is not known as a concept to the FileChooser class itself. It only plays a role when placed in a dialog or separate stage.

Oliver-Loeffler avatar Nov 13 '22 09:11 Oliver-Loeffler