Modal dialog hide behind Main Frame after swich focus
I have a swing application, basically, a main frame that could pop up a modal dialog.
When the modal dialog is showing, if I switch to another window, like firefox. And then switch back to the swing application. The JDialog
is not in front any more.
I don't want to set the dialog AlwaysOnTop to be true. because then the dialog will be on top of all windows include windows in other process.
So what should I do so that when I swich back, the modal dialog still on top?
BTW: it is a Applet, so the main frame is actually be set in this way:
private static Frame findParentFrame(Container owner){
Container c = owner;
while(c != null){
if (c instanceof Frame)
return (Frame)c;
c = c.getParent();
}
return (Frame)开发者_C百科null;
}
Make sure that the JDialog
is actually modal. Also try setting the main frame as the owner.
I am not sure if the modality of the dialog is the key issue here. I have tested this behaviour and the dialog always popups on the front when the app is maximised independent of it being modal.
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
public class AppletTest extends JApplet
implements ActionListener
{
private static final long serialVersionUID = 1L;
private Frame findParentFrame()
{
Container c = this;
while(c != null)
{
if(c instanceof Frame)
return (Frame) c;
c = c.getParent();
}
return (Frame) null;
}
private void createGUI()
{
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
JDialog d = new JDialog(findParentFrame());
d.setModal(true);
d.setVisible(true);
}
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
}catch(Exception e)
{
System.err.println("createGUI didn't successfully complete");
}
}
@Override
public void actionPerformed(ActionEvent e)
{
}
}
Check out the example I provided. You can comment the line with d.setModal(true);
and the result will be exactly the same.
I would suggest for you to check your code once more or show it to us as it seems that you might have missed something there.
PS: I found on the web some other hack-like solution http://www.codeguru.com/forum/showthread.php?t=41536 I would still focus on checking your code though.
Oi & Good luck, Boro.
I think what you're asking is for a dialog that's modal to the Java application/frame that is it's parent. When the parent regains focus you can use Toolkit.getDefaultToolkit().getSystemEventQueue(). postEvent(AWTEvent e) to fire an event to the dialog to make it pop back to the top.
Thanks Boro for the link!
I had identical problem I needed to solve. Browser with a swing applet. Dialog pops up, I click on the browser, click back on dialog and dialog disappears behind the browser. I tried everything, but only one thing helped:
Adding WindowListener
to Dialog
and call to toFront()
in listener's windowDeactivated()
worked for me.
精彩评论