11 Replies Latest reply: Jun 23, 2010 10:03 AM by PhHein RSS

    Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)

    843802
      We've had problems with invisible applets stealing focus from HTML form fields with MSIE 6 when using the Java plug-in. Unfortunately, Sun has been unable to duplicate the problem I reported. Just curious to see if anyone else has seen this problem?

      Thanks,
      Daniel Rabe


      synopsis: Invisible applet steals focus from HTML form field (MSIE, Sun plug-in only)
      description: FULL PRODUCT VERSION :
      java version "1.4.2_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06) Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)

      FULL OS VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      MSIE 6.0.2800.1106.xpsp2.030422-1633

      We've seen this happening for quite some time, so the specific version of IE probably isn't critical. We've also seen it in previous versions of the Java plug-in, including 1.3.x.

      A DESCRIPTION OF THE PROBLEM :
      The attached files show how an invisible applet "steals" focus from a form element. We have an applet on a page that performs some processing when called from JavaScript, but we don't want the applet itself to be visible on the page. Fortunately, we can disable its display by using style="display:none". However, when this applet is on the page, it grabs focus away from the HTML form field. It seems reasonable to us that this would be okay if the applet is visible, but if the applet isn't even being displayed, it shouldn't happen.

      Enclosed:
      TestApplet.java - a very simple applet.
      good.html - An HTML file with a single form field. An onload script sets the focus to the form field. As soon as the page comes up, you can start typing, and the characters go into the form field. This is good. bad.html - Same as good.html, with the addition of an invisible applet. When it loads, you can see the cursor flash momentarily in the form field, but then if you start typing your keystrokes are lost. This is bad.

      Note that the problem does not occur when using the Microsoft VM (5.0.0.3810), nor does it occur when using Netscape 7.1. Therefore, it appears to be an interaction between MSIE 6 and the Sun Plug-in.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See description above. Just open bad.html in MSIE 6. When the page comes up, start typing. The characters go nowhere.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When the page comes up, the characters you type should go into the form field. ACTUAL - When the page comes up, the characters you type go nowhere.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      ---TestApplet.java---
      import java.applet.*;

      public class TestApplet extends Applet
      {
           public TestApplet ()
           {
                System.err.println( "TestApplet" );
           }

           public void init()
           {
                System.err.println( "TestApplet init" );
           }
           public void destroy()
           {
                System.err.println( "TestApplet destroy" );
           }
      }

      ---

      ---good.html---
      <html>
      <body onload="document.TestForm.TestField.focus()">
      <form name="TestForm">
      Field: <input name="TestField" size="40">
      </form>
      </body>
      </html>

      ---


      ---bad.html---
      <html>
      <body onload="document.TestForm.TestField.focus()">
      <applet code="TestApplet.class" width="2" height="2" style="display:none"></applet> <form name="TestForm">
      Field: <input name="TestField" size="40">
      </form>
      </body>
      </html>

      ---
      ---------- END SOURCE ----------
        • 1. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
          843802
          I experienced the same problem using IE 6, an invisible applet and a frame set. To solve the problem I had the applet load but not start. After the page requiring focus had loaded I used JavaScript to start the applet.
          • 2. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
            843802
            I have kinda same problem.

            I have a top frame with a menu applet in it.
            Bottom frame is a form with several text fields.
            Normally the focus is set on the first text field of the bottom frame so that the user can start typing as soon as he sees the page.

            Now the thing is. If I use the JVM from Microsoft then the focus is left on the text field (like its supposed to be). But if I use the the JVM from Sun then the applet steals the focus from the text field. That really sucks.

            Can someone help? Haven't found much info about this problem.

            Thx. Christophe
            • 3. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
              843802
              I also encountered this issue. My workaround was to set a timeout on window load that sets the focus back to my element.
              <SCRIPT LANGUAGE="javascript" type="text/javascript" FOR="window" EVENT="onload">
                   window.setTimeout("document.body.focus()", 1000);
              </SCRIPT>
              • 4. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                843802
                I experienced similar "focus" related problems, please kindly see:
                http://forum.java.sun.com/thread.jsp?forum=30&thread=517485 (window with applet tag gains focus prior to new window browser window)

                one of the comments made in response to the topic (Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)) (by user Scotty58us) was to and I quote:

                "To solve the problem I had the applet load but not start. After the page requiring focus had loaded I used JavaScript to start the applet. "

                can anyone elaborate on how such a delayed start of the applet would be done? (via JS, applet code, etc?)

                appreciate in advance
                • 5. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                  843802
                  I have the same problem, but apart from that I have one another too.
                  COming to the first problem, calling JS functions from applet only after completely loading the HTML body

                  1) If it is possible position the OBJECT or APPLET Tag at the end of the body, since this is Invisible applet, it really doesnt need to position the tag only at certain point in the body. You could as well put that tag at the bottoom. Not sure how much % this would solve the problem, but as per my understanding of how browers parse tag by tag and make every tag ready to render, as this APPLET or OBJECT tag is at the last and by then rest all GUI HTML Form elements would have been processed to render. So a minimum chances of applet making reference/work on to YET TO PARSE ANY FORM ELEMENT.

                  2) I am thinking on doing a delayed start like this. I have a dummy applets, init and start method. Say I move my prev init(){....} code to delayedInit(){......} and this method now will be called from onLoad event or onAfterLoad event of the HTML Body.
                  <body onLoad="document.MyApplet.delayedIniti()">. As MyApplet is invisible applet, ignoring the use of start and stop methods of applet is not a big deal rt. and more over Applets running under plug in is really different and is not garunteeing that when browser minimizes/maximizes, applets start and stop is called. So I tradeoff that for this delayedInit :-)

                  Any one if you feel that my process is wrong and there is yet another ensured way of making applet's init and start gets called only after HTML is completely rendered, please help us on it. I am making some JS calls in the initi method, so some times I am running in to JS Exceptions and my first guess is that particular HTML Form Element wouldnt have been renderd at the time of applet trying to make a reference.

                  ---------------
                  Second:
                  I am worrying why if there is ONE JS Error occured in the init or start method of applet, and I have to make another call (but to another JS method), I am getting that and rest all JS Methods call run in to Exception until that start or init method is done. Even though I handle exception after each JS Call from APplet, this accumuliation of chained JS Exceptions are not preventable. Say i have to call 5 JS Different Methods from applets start method. For some reasons 1st JS call run in to exception, I handle the exception and proceed to call second one. Second one also throws exceptions and so on until 5, end of start method. Later on if user interacts or forces a JS call - thos things will work :-(
                  • 6. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                    843802
                    FYI, Sun was finally able to duplicate the problem. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4984794.
                    • 7. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                      843802
                      If anyone is interested, I believe I found a workaround for this.

                      In your applet start() method, add the following:

                      JPanel panel = new JPanel();
                      this.add(panel);
                      this.addFocusListener(new FocusAdapter() {
                      public void focusGained(FocusEvent e) {
                      JSObject win = (JSObject) JSObject.getWindow(this);
                      win.eval("onAppletLoad();");
                      }
                      });
                      panel.requestFocusInWindow();


                      Then, within your HTML code, add the following function:

                      function onAppletLoad() {
                           myField.focus(); //where myField is the field to get focus!!
                      }

                      Although the applet has a JPanel, just set the size to 0,0 and you won't see it.

                      Hope this helps

                      Shawn
                      • 8. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                        843802
                        Further to my last post, you'll have to import the following in the applet

                        import netscape.javascript.*; // JSObject class, used to get the HTML page

                        and add plugin.jar to your classpath. You'll find plugin.jar within your JRE /jre/lib directory (1.4.x+)
                        • 9. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                          843802
                          Sorry folks,

                          I noticed a bug in my applet code:
                          Here's the right code:


                          JPanel panel = new JPanel();
                          this.add(panel);
                          panel.addFocusListener(new FocusAdapter() {
                          public void focusGained(FocusEvent e) {
                          JSObject win = (JSObject) JSObject.getWindow(this);
                          win.eval("onAppletLoad();");
                          }
                          });
                          panel.requestFocusInWindow();
                          • 10. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                            843802
                            Solution:
                            This behavior is the default since JDK 1.3. However you might need to prevent the applet from getting focus on startup, for example, if your applet is invisible and does not require focus at all. In this case, you can set to false the special parameter initial_focus in the HTML tag, as follows:

                            <applet code="MyApplet" width=50 height=50>
                            *<param name=initial_focus value="false">*</applet>

                            Regards
                            Nagarajan.P
                            Lisec Software
                            • 11. Re: Invisible applet steals focus from HTML form field (MSIE, Java plug-in 1.4)
                              PhHein
                              NagaLisec, welcome to the forum. Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

                              I'm locking this thread now.