问题
I created a swing application on Windows OS. One of my JDialog (whose window is parentJFrame) shows a JOptionPane.JOptionPane.showMessageDialog(parentJFrame, "I am a JOption");
.
At the run time, the parentJFrame setAlwaysOnTop(true)
. Even though it has set alwaysOnTop-TRUE, the JOptionPane appeares on the parentJFrame on Windows OS. but When I ran it on Linux OS,JOptionPane displays behind the parentJFrame.(May be the reason is that parentJFrame alwyasOnTop is true, but how JRE runs the same application in different ways for diffrent OS s ?) How can I get it on the top of parentJFrame in Linux.? This is urgent please..
When a 'alwaysOnTop-true' component shows a JOptionPane, JOptionPane appears behind the component in Linux.. :( . But this handled well in Windows OS. JOptionPane is showed on the top of the component which is set 'alwaysOnTop-true' . It seemed that, there is a conflict Showing components on the desktop screen in Linux OS.. I m not sure so. But I guess it.
- Linux OS has Oracle JDK and JRE 7
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/11598913/joptionpane-displays-behind-the-parent-jframe