JOptionPane displays behind the parent JFrame

别等时光非礼了梦想. 提交于 2019-12-06 08:06:34

The behaviour you see on Linux is in accordance with the API specification. This is what it says for Window.setAlwaysOnTop():

If there are multiple always-on-top windows, their relative order is unspecified and platform dependent.

And also:

All windows owned by an always-on-top window inherit this state and automatically become always-on-top.

Which would explain why the JDialog that's at the heart of JOptionPane also has "always on top" status. Seems that on Windows by chance it works as you expected, but really you're asking Swing to do something impossible: To show the parent "always above other windows", but also to show the dialog on top of it.

Here's a possible workaround: Place the dialog next to the parent, so that while it's under it on the z-axis, the user will still see it:

JDialog dialog = new JOptionPane("Message").createDialog(parent, "Title");
Point dialogLoc = dialog.getLocation();
Point parentLoc = parent.getLocation();
dialog.setLocation(parentLoc.x + parent.getWidth(), dialogLoc.y);
dialog.setVisible(true);

Do note that there is no single "Linux OS", especially when it comes to window management - there are lots of different desktop environments and window managers that behave in widely different ways when it comes to window ordering and visibility, often deliberately.

This is very Simple : write this line of code after the code which you want to show ddialog box:

        JOptionPane optionPane = new JOptionPane("Reports are Generated");
        JDialog dialog = optionPane.createDialog("Success!");
        dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
        dialog.setVisible(true);

Dont change anything exept Strings in double quotes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!