Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a modal dialog in Java applet?

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?

Ideally this should be for Java versions 1.5 and above.

like image 253
Nick Avatar asked Oct 17 '10 23:10

Nick


People also ask

What is modal dialog box in Java?

Modal dialog box — A dialog box that blocks input to some other top-level windows in the application, except for windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press.

What are modes of dialog in Java?

There are two basic types of dialogs: modal and modeless. Modal dialogs block input to other top-level windows. Modeless dialogs allow input to other windows. An open file dialog is a good example of a modal dialog.

How do you make a dialogue box in Java?

How to Create a Dialog Box in Java. There are several ways in which a developer can create a dialog box in Java. Programmers can use JDialog, JOptionPane, or ProgressMonitor. To create a standard dialog, you can simply use the JOptionPane class.

What is a modeless dialog box?

Modal dialog boxes, which require the user to respond before continuing the program. Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities.


3 Answers

JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(200, 200);
dialog.setVisible(true);
like image 85
camickr Avatar answered Oct 06 '22 20:10

camickr


Frame f =(Frame)SwingUtilities.getAncestorOfClass(Frame.class,parentWindow); new JDialog(f,true);

(source = http://kb.trisugar.com/node/7613) works for parentWindow = sun.plugin2.main.client.PluginEmbeddedFrame

like image 27
BenB Avatar answered Oct 06 '22 21:10

BenB


Use null insterad of applet.getMyParent()

like image 26
Ed.C Avatar answered Oct 06 '22 20:10

Ed.C