失去焦点后,Javafx弹出窗口不会隐藏在其他应用程序后面

问题描述:

所以我的问题,正如标题中所述(某种类型),是关于Javafx(即2.2)Popup在某些方面的行为。大多数情况下,你得到一个弹出窗口,你给它一个窗口作为它的父母,你给它一些场景,它往往相对独立。

So my question, as stated (sort of) in the title, is about the behaviour in some respects of the Javafx (namely 2.2) Popup. Most of the time, you get a popup, and you give it a window to act as it's parent, you give it some scene, and it tends to act relatively independently.

这一切都很好,但是,在我的情况下,我需要一个弹出窗口,它会将自己锚定到某个特定位置(窗口),特定位置,当一个事件发生了。然后,当窗口消失时(最小化,屏幕外,无论如何),弹出窗口将会消失,当它完成时移动,并且在所有的本质和功能中,只是具有自定义形状的窗口的物理扩展。

This is all fine, however, in my case, I needed a popup that would anchor itself to a particular stage (window), at a particular location, when an event happened. That popup would then, in turn, disappear when the window disappeared (minimize, off screen, whatever), moved when it would, and in all essence and functionality, be a physical extension of the window, just with a custom shape.

当然,这有很多细微差别,而且大部分内容都很有效。我似乎无法弄清楚的唯一一件事就是通常在像Windows 7 64位这样的平台上。你打开两个程序,好吧。然后,如果程序重叠一点,无论哪个有焦点都可以显示整个程序,而另一个给人的印象是在另一个窗口后面。 (当另一个人专注于同一个地方时,无论窗口是否真的将应用程序图形渲染到窗口后面,我都不确定。)通常,javafx也支持这个功能。但出于某种原因,javafx中的Popup类(请参阅文档此处)不这样做。无论如何,它总是在它显示的任何内容之上。为了完整性,这里是我非常简单的弹出代码(至少有关于显示它和它的属性的部分):

Now of course, there are a lot of nuances with that, and everything for the most part, works great. The only thing I can't seem to figure out is that normally in a platform like Windows 7 64 bit, say. You open two programs, alright. Then if the programs are overlapping a little bit, whichever has focus gets to have the entire program displayed, whereas the other one gives the impression of being 'behind' the other window. (Whether or not windows actually renders application graphics 'behind' a window when another has focus on the same spot, I'm not sure.). Normally, javafx also supports this functionality just fine. But for some reason, The Popup class in javafx (see docs here) doesn't do that. It's always on top of whatever it's displayed with, without exception. For the point of completeness, here's my pretty straightforward popup code (at least the part pertaining to showing it and it's properties):

        Popup myPop = new Popup();
        //************************Popup Property Setter**************************
        //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        myPop.setWidth(width);
        myPop.setHeight(initialHeight);
        myPop.setAutoHide(false);
        myPop.setAutoFix(false);
        myPop.setHideOnEscape(false);
        myPop.setX(xLocation);
        myPop.setY(yLocation);
        //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        //**********************end of Popup Properties**************************
        myPop.getContent().add(main_anchor);
        myPop.show(FileChooserWindow.get_stage());

主锚包含我在'myPop'弹出窗口中包含的各种组件,而FileChooserWindow是非-null父窗口,在此方法调用时将打开,无异常。

main anchor has some various components i include inside of the 'myPop' popup, and FileChooserWindow is a non-null parent window that will be open at the time of this method calling without exception.

以下是我所指的行为的截图。请注意pdf中突出显示的文本,即我的光标当前具有焦点的位置。此外,弹出窗口固定的窗口可以在从左侧伸出的pdf背面看到。

Here is a screenshot as well of the behaviour I'm referring to. Notice the highlighted text in the pdf, that is where my cursor currently has focus. Also, the window that the popup is anchored to can be seen in the back of the pdf poking out from the left.

任何帮助你们可以给予非常感谢。我真的希望我不必检查活动进程以及它们相对于弹出窗口的位置,这种方式越来越接近我的知识边界,听起来就像一个完整的PITA。

Any help you guys can give would be much appreciated. I really hope I don't have to check for active processes and their location relative to the popup, that's getting dangerously close to my knowledge border, and sounds like a total PITA.

所以,经过几天的玩弄,我有一个粗略的解决方法,虽然这是一个黑客攻击这个词的全部含义。

So, after toying with this for a few more days, I have a rough workaround, although it is a hack in the full meaning of the term.

虽然弹出行为仍然让我神秘,但我可以通过将changeListener添加到它所连接的舞台来模拟此行为中的修复(因为我没有希望弹出窗口关闭,如果它的父窗口有焦点,只有当弹出窗口和它的父窗口有焦点时才会关闭)。

Although the popup behaviour is still mystifying me, I can simulate a fix in this behaviour by adding a changeListener to the stage to which it is attached (since I didn't want the popup to close if it's parent window had focus, only if anything BUT the popup and it's parent got focus).

请参阅以下代码:

FileChooserWindow.get_stage().focusedProperty().addListener(new ChangeListener<Boolean>(){
            @Override
            public void changed(ObservableValue<? extends Boolean> ov, Boolean oldValue, Boolean newValue) {
                if (!newValue){
                    if(AutogenPopup.popupReturner().focusedProperty().get()){
                        AutogenPopup.popupReturner().hide();
                    }
                }else{
                    if(FileChooserController.refreshAutoPopup && FileChooserController.autoGen_flag){
                        AutogenPopup.popupReturner().show(FileChooserWindow.get_stage());
                    }
                }
            }

        });

从不介意我正在检查的一些标志,它们只是一些内部工具确保只有在程序处于正确状态时才会出现弹出窗口。

never mind some of those flags that I'm checking against, they are simply some internal tools to make sure that the popup only appears when the program is in the proper state.

现在,有一点值得注意。 AutogenPopup.popupReturner()。focusednedProperty()。get()
当弹出窗口的父窗口LOST焦点时,似乎返回true。这对我来说是非常反直觉的,而且在我看来,甚至是一个触摸手推车。现在,这并不模拟现代操作系统的行为,其中窗口可以逐渐滑入和滑出焦点,因为弹出窗口将在父窗口失去焦点时完全消失。但是看看我的弹出窗口如何在主窗口侧面显示额外的文本条目,这是一个可接受的折衷方案,直到我想到一个更好的解决方案。 (也许更好的解决方案是根本不使用弹出窗口,而是将窗口设置为以弹出方式操作)。

Now, one interesting thing to note. The AutogenPopup.popupReturner().focusedProperty().get() Seemed to be returning true when the popup's parent window LOST focus. Which is quite counter-intuitive to me, and in my opinion, is even a touch buggy. Now, this does not simulate the behaviour of a modern operating system where a window can slide in and out of focus incrementally, since the popup will just completely disappear upon it's parent window losing focus. But seeing as how my popup just displays additional text entry on the side of the main window, this is an acceptable compromise until I think of a better solution. (Perhaps the better solution is not to use a popup at all, and instead skin a window to act in a popup-y fashion).

无论如何,我希望这对某人有所帮助,也许最终会有一种更精细的方式来控制这个弹出功能。

Anyway, I hope this helps someone, maybe eventually there will be a more fine-grained way to control this popup functionality.