Modal JDialog in JApplet
This may be a really stupid question but how do I make a modal JDialog in an JApplet that is properly tied to the applet.
JDialog has constructors like
JDialog(Frame owner, String title,boolean modal)
or instead of a Frame another Dialog.
But how do I get that from a JApplet?
Because otherwise I can make it modal but it's isn't right so that if you go to another window/application and then go back the dialog can be hidden.
To be perfectly honest when I pass it null it is broken in appletviewer (I get an applet that is locked by modal but the dialog is hidden somewhere) when I use this in browsers (IE and FireFox) it doesn't seem to be a problem for some reason. Going back to the browser brings the modal dialog to the front. But I don't want to rely on something that is wrong.
I have looked through the docs and even the tutorials and I don't see how one does this.
I have also searched but the answers like in this thread http://forum.java.sun.com/thread.jspa?forumID=57&threadID=751860 don't help because I don't know how to get the parent frame.
I should also probably mention that for compatibility reasons it would be best if the solution was 1.4 compatible.
[1282 byte] By [
cotton.ma] at [2007-11-27 10:58:47]

# 1
Actually I just realized I lied about one thing. I pass it (the JDialog constructor) new Frame() and not null.
# 2
The best you can do is either
1. Pass null and hope;
or
2. Use JInternalFrame rather than Dialog, and play silly games to make it modal. (Overriding the event queue would probably suffice, although it'll need to be a signed applet).
# 4
Try this to get the frame. The applet is supposed to be in a frame at some level. Modality should work relative to that.
Frame f = JOptionPane.getFrameForComponent(applet);
# 5
> Try this to get the frame. The applet is supposed to
> be in a frame at some level. Modality should work
> relative to that.
>
> Frame f = JOptionPane.getFrameForComponent(applet);
Interesting....
Doing this inside my applet code
Frame f = JOptionPane.getFrameForComponent(this);
System.out.println(f);
I get this in the console (in the browser)
sun.plugin.viewer.frame.IExplorerEmbeddedFrame[frame0,0,0,680x400,invalid,layout=java.awt.BorderLayout,title=,resizable,normal]
Well that looks promising at least. I'm going to try actually using it now...
# 6
something like this seems to work OK (single test only)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestApplet extends JApplet
{
public void init()
{
JButton btn = new JButton("Show JDialog");
JPanel p = new JPanel();
p.add(btn);
add(p);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane op = new JOptionPane("Hello World");
JDialog d = op.createDialog(TestApplet.this,"title");
d.setVisible(true);
}
});
}
}
# 7
Okay JOptionPane.getFrameForComponent(this); works beautifully. And I guess that would be that the same thing that Michael_Dunn's code is doing.
Thanks to all. It's working great.
When I say great it works the same as it was in the browser but it's now working properly in appletviewer as well. Which make me more comfortable.
# 8
Michael_Dunn's code works, but it creates a JOptionPane for no reason (not no reason, but it's unused otherwise), and JOptionPane.createDialog uses the static getFrameForComponent() method internally anyway.
> When I say great it works the same as it was in the browser
> but it's now working properly in appletviewer as well. Which
> make me more comfortable.
Is that to mean that it doesn't work correctly? I have an applet that uses JOptionPane for an about dialog, and that blocks everything... in FireFox, at least.
Message was edited by:
bsampieri
# 9
> Michael_Dunn's code works, but it creates a
> JOptionPane for no reason (not no reason, but it's
> unused otherwise), and JOptionPane.createDialog uses
> the static getFrameForComponent() method internally
> anyway.
>
> > When I say great it works the same as it was in the
> browser
> > but it's now working properly in appletviewer as
> well. Which
> > make me more comfortable.
>
> Is that to mean that it doesn't work correctly?
No. That is to say that bottom line I am using your method and it is working perfectly.
What I was also trying to say was that before I posted this thread I noticed that it was working correctly in the browser (unexpected) but was not working correctly in appletviewer (expected). So I was a might bit concerned that your solution would work in appletviewer but break on the browser (which is doing it's own voodoo in my mind).
But it works perfectly on both.
And this makes me happy because when it is working in appletviewer I have some degree of confidence that it will work on browsers I haven't tested yet.
# 10
Passing in null will create (utimately) a shared frame from SwingUtilities to use as a base. I thought maybe that the shared frame and the "browser" frame are the same in the browser, but a quick test shows that's not the case.
So I'm not sure why null would work in the browser, actually. Oh well...
# 11
Since a Java applet has no control over its parent's event handlers, it is impossible to create a truely modal dialog box that behaves like the browser's own dialog box. Basically what this means is that it is possible for the user to click on other area of the document while your applet is still waiting for a response.
It has always been like this.
;o)
V.V.
# 12
Actually, it appears to depend on the browser. The HTML part of IE7 doesn't get blocked, but the applet does. FireFox blocks up everything.