JavaFX FileDialog focus

Viewed 406

I am now trying to make focus to FX FileDialog. When i click outside the dialog, dialog is outfocused. Its any way to make when i click outside, the dialog call any metod that make him visible (focused)? TY :)

I just tried any like this.

...focusedProperty().addListener((obs, oldVal, newVal) -> System.out.println(newVal ? "Focused" : "Unfocused"));

and maeby this way...

fileChooser.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>(){
                        @Override
                        public void handle(WindowEvent window)
                        {...
1 Answers

There is no way to handle focus using addEventHandler inside javafx.stage.FileChooser, But you can simply use primaryStage.setAlwaysOnTop(true); to make your FileChooser always visible on top of other windows

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


public class JavaFXtest5 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Test");

        FileChooser chooser = new FileChooser();

        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                primaryStage.setAlwaysOnTop(true);
                chooser.showOpenDialog(primaryStage);
                primaryStage.setAlwaysOnTop(false);
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello Dialog!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
Related