Modal JDialog disappearing behind parent on solaris CDE
My code contains a JFrame which after a certain action, shows a non-modal JDialog. The user is expected to drag an object from the JFrame into the JDialog. The issue I'm having is only showing up on Sola开发者_高级运维ris CDE (Common Desktop Environment): opening JDialog correctly positions the window on top of the frame. After the user clicks on the frame, dialog disappears behind it forcing the user to re-position the frame to put it besides JDialog. The expected behavior is for the JDialog to remain on top of the parent frame.
The following code demonstrates the situation:
public class MyFrame extends JFrame
{
public MyFrame()
{
JButton btn = new JButton("Push me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JDialog dialog = new JDialog(MyFrame.this);
dialog.getContentPane().add(new JLabel("I'm a dialog!!!"));
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
});
getContentPane().add(btn);
pack();
}
public static void main(String args[])
{
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}
This problem is not when running any other window manager on solaris as well as windows and linux (GNOME). A similar question has been asked some time ago (How to make modeless dialog stay on top of its parent in Solaris CDE), but it remains unresolved.
Both JFrame
and JDialog
inherit the following Window
convenience methods: toFront() and toBack(), although JDialog
inherits the latter from Dialog
. Anyway, try using either of those in combination with a WindowListener by listening for the following events: windowActivated(WindowEvent e) and windowDeactivited(WindowEvent e).
Edit:
This what someone else suggested,
attach a focus listener on the JDialog and when it loses focus, move it to the front of all the other windows. This will cause flickering and works pretty horribly.
精彩评论