javafx - 警报和阶段焦点

时间:2017-07-07 14:20:01

标签: java javafx focus javafx-8 alert

对于我的应用程序,我需要知道应用程序的窗口是否正在聚焦。为此,我可以使用primaryStage.focusedProperty().addListener(..),这将警告我舞台焦点的变化。

但我已经意识到以Alert作为所有者打开primaryStage并将模态设置为WINDOW_MODAL会使primaryStage松散焦点(即使窗口位于现实集中,或至少在Windows中)。

现在我遇到的问题是,我想知道Window何时关注,而不仅仅是primaryStage;或至少知道Alert是否正在集中,但我找不到如何。

我尝试在警报上使用类似的属性(例如onShowingonHiding)但没有成功。

这是一段代码来说明我的问题:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));

        primaryStage.focusedProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("primaryStage focused : "+newValue);
        });
        primaryStage.show();

        //create a basic alert
        Alert alert = new Alert(Alert.AlertType.INFORMATION,"This is a test");
        alert.initModality(Modality.WINDOW_MODAL); //will block input to its owner window
        alert.initOwner(primaryStage);
        alert.onShowingProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("alert onShowing : "+newValue);
        });
        alert.onShownProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("alert onShown : "+newValue);
        });
        alert.onHidingProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("alert onHiding : "+newValue);
        });
        alert.onHiddenProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("alert onHidden : "+newValue);
        });
        alert.showAndWait();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

基本上它会打印出来:

primaryStage focused : true //stage is created and displayed
primaryStage focused : false //alert is displayed, stage loses focus. alt+tab changes nothing
primaryStage focused : true //alert closed by pressing 'ok'

由于它应该产生的所有其他印刷品,这很奇怪。 理想情况下我还需要:

primaryStage focused : true //stage is created and displayed
primaryStage focused : false //alert is displayed, stage loses focus
alert focused : true //alert gains focus
alert focused : false //alt+tab to an other window
alert focused : true //alt+tab back to this window
alert focused : false //alert closed by pressing 'ok'
primaryStage focused : true //stage regains focus

或类似的东西。有没有人有想法实现这一目标,或者primaryStage是否会将焦点转移到WINDOW_MODAL Alert我应该报告的问题上?

1 个答案:

答案 0 :(得分:0)

所以最后我找到了一个解决方法。警报中使用的每个buttonType都可以确定它是否已被聚焦。当alt + tabbing时,此属性也会更改,因此我们可以监控所使用的每个buttonType,并查看是否只有一个是焦点。

在这里我的解决方案,有点hacky但实现了我想要的目标:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));

        primaryStage.focusedProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("PrimaryStage focused : "+newValue);
        });
        primaryStage.show();

        //create a basic alert
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION,"This is a test");
        alert.initModality(Modality.WINDOW_MODAL); //will block input to its owner window
        alert.initOwner(primaryStage);

        alert.getButtonTypes().forEach(buttonType -> {
            //add a focus listnener for each buttonType of this alert
            alert.getDialogPane().lookupButton(buttonType).focusedProperty().addListener((observable, oldValue, newValue) -> {
                System.out.println(buttonType.getText()+" focused : "+newValue);
                System.out.println("Alert focused : "+isAlertFocused(alert));

            });
        });
        alert.showAndWait();
    }

    /** Looks all {@link ButtonType} used in the given {@link Alert} and checks if any of them is
     * focused, hence if the {@link Alert} is being focused
     *
     * @param alert the {@link Alert} we want to check the focused status from
     * @return true if the alert is focused, false otherwise
     */
    private boolean isAlertFocused(Alert alert){
        if(alert == null){
            return false;
        }

        final boolean[] focused = {false};
        alert.getButtonTypes().forEach(buttonType -> {
            focused[0] = focused[0] || alert.getDialogPane().lookupButton(buttonType).isFocused();
        });
        return focused[0];
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我想我也可以通过在alert.getDialogPane().isFocused()方法中添加isAlertFocused(Alert alert)的检查来将此方法扩展到对话框,但这不在我的问题范围内。