views:

292

answers:

3

I have a web page with an applet that opens a popup window and also makes Javascript calls. When that Javascript call results in a focus() call on an HTML input, that causes the browser window to push itself in front of the applet window. But only on certain browsers, namely MSIE. On Firefox the applet window remains on top. How can I keep that behavior consistent in MSIE? Note that using the old Microsoft VM for Java also achieves the desired (applet window in front) result.

HTML code:

<html>
    <head>
     <script type="text/javascript">
      function focusMe() {
       document.getElementById('mytext').focus();
      }
     </script>
    </head>
    <body>
     <applet id="myapplet" mayscript code="Popup.class" ></applet>
     <form>
      <input type="text" id="mytext">
      <input type="button" onclick="document.getElementById('myapplet').showPopup()" value="click">
     </form> 
    </body>
</html>

Java code:

public class Popup extends Applet {
    Frame frame;
    public void start() {
     frame = new Frame("Test Frame");
     frame.setLayout(new BorderLayout());
     Button button = new Button("Push Me");
     frame.add("Center", button);
     button.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
       frame.setVisible(false);
      }
     });
     frame.pack();
    }
    public void showPopup() {
     frame.setVisible(true);
     JSObject.getWindow(this).eval("focusMe()");
    }
}
A: 

The real question is why are you using an applet? It's been a while since I've done Java, but unless you've stripped out a lot of code from your example, it looks like you're just doing a simple dialog box. If that is the case, this can done easily and cheaply through any number of JavaScript libraries like Dojo and jQuery for example.

Justin Johnson
Yes, it's stripped down. The real applet is legacy code written many years ago before Ajax was even coined. I'm certainly considering switching this to a pure web-based implementation but that would take a fair amount of coding.
Dan
I was afraid that would be the case. Depending on what the dialog does, you may want be able to get away with pawning some of the work off on an existing JavaScript library. Here's a short list of the many different JavaScript dialog tools available: http://stackoverflow.com/questions/1884096/modern-windowless-popups/1884343#1884343
Justin Johnson
A: 

// JSObject.getWindow(this).eval("focusMe()"); frame.requestFocuse();

by change to these should word.

alin
A: 

We should encourage Oracle to support applets in web browsers more. After all, they are trying to push JavaFX technology which is applet technology. As far as focus isses are concerned, this seems to be complex. What about getting the basics rigt? Please refer to Applet steals Focus as a start.