views:

81

answers:

2

I'm trying to display a modal dialog in front of an Applet.

My current solution fetches the root frame like so:

Frame getMyParent() {
    Container parent = getParent();
    while (!(parent instanceof Frame)) {
        parent = ((Component)parent).getParent();
    }
    return (Frame)parent;
}

And creates the dialog as follows:

public OptionsDialog(MainApplet applet, boolean modal) {
    super(applet.getMyParent(), "options", modal);
    // ....

However often this shows the modal dialog below the frame, though the modal behaviour works correctly.

How can this be fixed?

EDIT: I should have clarified before, for Java versions 1.5 and above.

+1  A: 

Use null insterad of applet.getMyParent()

Ed.C
Hmmm, using null will set the owner frame to `SwingUtilities.getSharedOwnerFrame()` - I will test, thanks!
Nick
Unfortunately this does not resolve the problem, I have only tested with Chrome and it consistently shows below the page.
Nick
A: 
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(200, 200);
dialog.setVisible(true);
camickr
Thanks camickr. Unfortunately this requires version 1.6 and I'm stuck with supporting Java 1.5 (as a lot of people still use this). Apologies for not clarifying, I have edited the question.
Nick
I'm not aware of any code I posted being unique to version 1.6.
camickr
I found the JDialog(Window) constructor is 1.6 specific: http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Window)
Nick
Ok, so the constructor that takes a Window is from 1.6, but there are many other constructors to use. I'm betting you are actually using a JFrame as the owner Window, so just cast the Window to a JFrame. There is a constructor thats takes a Frame and a JFrame extends Frame.
camickr
Thanks camickr. Sorry for the delay. I tried testing to see whether windowForComponent was a JFrame, but no luck `java.lang.ClassCastException: sun.plugin2.main.client.PluginEmbeddedFrame cannot be cast to javax.swing.JFrame`
Nick