【问题标题】:JavaFX FileDialog focusJavaFX FileDialog 焦点
【发布时间】:2017-11-23 09:42:51
【问题描述】:

我现在正试图将注意力集中在 FX FileDialog 上。当我在对话框外单击时,对话框聚焦。当我在外面点击时,它有什么方法可以让对话框调用任何使他可见(聚焦)的方法?泰:)

我只是尝试过这样的。

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

然后就这样……

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

【问题讨论】:

    标签: java javafx javafx-8 filedialog


    【解决方案1】:

    javafx.stage.FileChooser 中使用addEventHandler 无法处理焦点, 但是您可以简单地使用primaryStage.setAlwaysOnTop(true); 使您的FileChooser 始终在其他窗口的顶部可见

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2014-11-15
      • 1970-01-01
      • 2019-04-21
      • 2016-04-09
      相关资源
      最近更新 更多