在javafx中显示工具提示使它进入了前台 [英] displaying a tooltip in javafx brings its stage into the foreground

查看:75
本文介绍了在javafx中显示工具提示使它进入了前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决jdk中的此错误: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8088624

I am trying to work around this bug in the jdk: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8088624

public class Blubb extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button btn = new Button("Click");
        btn.setTooltip(new Tooltip("Blubb"));

        Scene scene = new Scene(new BorderPane(btn), 320, 240);
        primaryStage.setScene(scene);
        primaryStage.show();

        Stage secondStage = new Stage();
        secondStage.setScene(new Scene(new BorderPane(new Button("Click")), 320, 240));
        //secondStage.initOwner(primaryStage);
        secondStage.show();
    }
}

如果将第一阶段的按钮悬停,它将出现在第二阶段的前面.我发现在舞台上调用initOwner()可以消除这种现象.

If the button on the primary stage is hovered, it will come in front of the second stage. I found that calling initOwner() on a Stage will eliminate this behavior.

现在我的问题是:我有多个具有共同所有者的弹出窗口"(主要阶段).在initOwner()解决方法之后,将鼠标悬停在主阶段的控件上不会导致任何意外的行为.但是,如果在另一个弹出窗口处于焦点状态时将鼠标悬停在弹出控件上,则悬停的弹出窗口将窃取焦点.

Now my problem is following: I have multiple "popups" that have a common owner (the primary stage). Hovering over controls on the primary stage doesn't cause any unexpected behavior after the initOwner() workaround. If you however hover over controls in a popup while another popup was in focus, the hovered popup will steal focus.

有没有一种方法可以解决这个错误,不仅适用于初级阶段,还适用于弹出窗口?

Is there a way I can work around this bug for not only the primary stage but also the popups?

更新:事实证明,我的解决方法具有不良的副作用.用于以下阶段状态的Javadocs:

UPDATE: turns out my workaround has undesired side-effects. Javadocs for Stage state following:

一个阶段将始终位于其父窗口的顶部.

A stage will always be on top of its parent window.

那么,还有什么变通办法可以使弹出窗口不总是位于顶部"并最小化?

So additionally, what would be a workaround that makes the popup not "always on top" and minimizable?

推荐答案

有一种方法可以通过叠加StackPanes来解决.用StackPane创建Scene,以便在舞台失去焦点时可以添加另一个StackPane.覆盖的窗格将防止Tooltip或鼠标悬停在焦点不对准时发生的任何其他情况.您也可以最小化任何阶段,并且它们不会总是最重要的.

There is a way to get around it by overlaying StackPanes. Create your Scene with a StackPane so that you can add another StackPane when the stage has lost its focus. The overlayed pane will prevent Tooltips or anything else happening on mouse-over while the pane is not in focus. You may also minimize any of your stages and they won't be always-on-top.

public class Blubb extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button_1 = new Button("Button #1");
        button_1.setTooltip(new Tooltip("Blubb #1"));

        StackPane primary = new StackPane(new BorderPane(button_1));
        primaryStage.setScene(new Scene(primary, 320, 240));
        addStageFocusListener(primaryStage, primary);
        primaryStage.show();

        Button button_2 = new Button("Button #2");
        button_2.setTooltip(new Tooltip("Blubb #2"));

        StackPane second = new StackPane(new BorderPane(button_2));
        Stage secondStage = new Stage();
        addStageFocusListener(secondStage, second);
        secondStage.setScene(new Scene(second, 320, 240));
        secondStage.show();
    }

    public void addStageFocusListener(Stage stage, StackPane stackPane) {
        stage.focusedProperty().addListener(new ChangeListener<Boolean>(){
            public final StackPane preventTooltip = new StackPane();
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if(stage.isFocused()) {
                    if(stackPane.getChildren().contains(preventTooltip)) {
                        stackPane.getChildren().remove(preventTooltip);
                    }
                } else {
                    stackPane.getChildren().add(preventTooltip);
                }
            }
        });
    }
}

这篇关于在javafx中显示工具提示使它进入了前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆