views:

715

answers:

4

I have to write an applet that brings up a password dialog. The problem is that dialog is set to be always on top but when user clicks on IE window dialog gets hidden behind IE window nevertheless. And since dialog is modal and holds all IE threads IE pane does not refresh and dialog window is still painted on top of IE (but not refreshed). This behaviour confuses users (they see dialog on top of IE but it looks like it has hanged since it is not refreshe).

So I need a way to keep that dialog on top of everything. But any other solution to this problem would be nice.

Here's the code:

        PassDialog dialog = new PassDialog(parent);
        /* do some non gui related initialization */
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setAlwaysOnTop(true);
        dialog.setVisible(true);

Resolution: As @shemnon noted I should make a window instead of (null, Frame, Applet) parent of modal dialog. So good way to initlialize parent was:

parent = javax.swing.SwingUtilities.getWindowAncestor(theApplet);
A: 

You might try launching a modal from JavaScript using the JavaScript integration (see http://www.raditha.com/java/mayscript.php for an example).

The JavaScript you would need would be something like:

function getPassword() {
  return prompt("Enter Password");
}

And the Java would be:

password = jso.call("getPassword", new String[0]);

Unfortunately that means giving up all hope of having a nice looking modal. Good luck!

noah
+1  A: 

Make a background Thread that calls toFront on the Dialog every 2 seconds. Code that we use (I hope I got everything):

class TestClass {
protected void toFrontTimer(JFrame frame) {
    try {
        bringToFrontTimer = new java.util.Timer();
        bringToFrontTask = new BringToFrontTask(frame);
        bringToFrontTimer.schedule( bringToFrontTask, 300, 300);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

class BringToFrontTask extends TimerTask {
    private Frame frame;
    public BringToFrontTask(Frame frame) {
        this.frame = frame;
    }
    public void run()
    {
        if(count < 2) {
            frame.toFront();
        } else {
            cancel();
        }
        count ++;
    }
    private int count = 0;
}

public void cleanup() {
    if(bringToFrontTask != null) {
        bringToFrontTask.cancel();
        bringToFrontTask = null;
    }
    if(bringToFrontTimer != null) {
        bringToFrontTimer = null;
    }
}

java.util.Timer bringToFrontTimer = null;
java.util.TimerTask bringToFrontTask = null;
}
James A. N. Stauffer
Its a bit to brute force to me. But it's the easiest choice.
jb
+1  A: 

This is a shot in the dark as I'm not familiar with applets, but you could take a look at IE's built-in window.showModalDialog method. It's fairly easy to use. Maybe a combination of this and Noah's suggestion?

Joel Anair
+1  A: 

What argument are you using for the parent?

You may have better luck if you use the parent of the Applet.

javax.swing.SwingUtilities.getWindowAncestor(theApplet)

Using the getWindowAncestor will skip the applet parents (getRoot(component) will return applets). In at least some versions of Java there was a Frame that was equivalent to the IE window. YMMV.

shemnon
Well I did something like JOptionPane.getRootFrame. I'll look into it first thing tommorow.
jb
Worked - thanks.
jb