HOWTO: Creating a Modal Dialog Box from Java

Last reviewed: January 29, 1998
Article ID: Q177753
The information in this article applies to:
  • SDK for Java, version 2.0, 2.01
  • Microsoft Internet Explorer (Programming), version 4.0, 4.01

SUMMARY

This article describes how to create a modal dialog box with respect to the Internet Explorer browser in Java. The class java.awt.Dialog allows you to create a modal dialog box, but this dialog box is not modal with respect to the browser. To create a modal dialog box with respect to the browser, you can use Internet Explorer 4.0's showModalDialog method (part of the Window object). Due to a limitation in calling showModalDialog directly from Java, a technique is used to call the showModalDialog method on behalf of the Virtual Machine for Java.

MORE INFORMATION

The following HTML and Java applet demonstrates how to create a modal dialog box from Java under Internet Explorer 4.0. This sample demonstrates invoking the showModalDialog method from Java using the setTimeout method and interaction between Java and Internet Explorer 4.0 using AutoIDispatch.

Please see the following Java code, which is used in HTML later. This applet has a method called setMyObject(), which takes an object as a parameter. When the user clicks the "click me" button, the "doModal()" method is invoked on the object passed in from the setMyObject() method. The "doModal()" method takes a string as a parameter, which is the content of the text field.

   import java.applet.Applet;
   import java.awt.*;
   import java.awt.event.*;
   import com.ms.com.Dispatch;

   public class DialogTest extends Applet implements ActionListener
   {
      Object MyObject;

      TextField tf= new TextField("testing.....",30);

      public void init()
      {
         Button b = new Button("click me");
         add(b);
         b.addActionListener(this);
         add(tf);
      }

      public void setMyObject(Object MyObject) { MyObject = MyObject; }

      public void actionPerformed(ActionEvent e)
      {
         Dispatch.call(MyObject, "doModal", tf.getText());
      }
}

The following HTML creates a Java applet called DialogTest and, upon initialization, passes in a reference to "this" via the setMyObject() method in the applet. The applet stores a reference to this object in order to call the method doModal, which in turn sets a timeout that is a workaround for calling showModalDialog directly. The argument passed into doModal() is passed down to showModalDialog as the dialog argument into the modal dialog box.

   <HTML>
   <HEAD>
   <TITLE>Dialog Test</TITLE>
   </HEAD>
   <BODY>
   <APPLET
      CODE=DialogTest
      ID=DialogTest
      WIDTH=300
      HEIGHT=70 >
   </APPLET>

   <SCRIPT LANGUAGE="JavaScript">
   <!--
   function doModal(tst)
   {
      setTimeout("doModal2('DialogTestDialog.html', '"+tst+"')",100);
   }

   function doModal2(url, dialogarg)
   {
      var strFeatures = "dialogWidth:385px;dialogHeight:100px;" +
         "help:no;maximize:no;minimize:no;scrollbars:no";
      var cRetValue=showModalDialog(url,dialogarg,strFeatures);
      if (cRetValue == null)
      {
         alert('You clicked the cancel or close button');
      }
      else
      {
         alert(cRetValue);
      }
   }

   function init()
   {
      DialogTest.setMyObject(this);
   }

   window.onload=init;
   -->
   </SCRIPT>
   </BODY>
   </HTML>

The following HTML is the DialogTestDialog.html referenced in the HTML above. This HTML effectively becomes the modal dialog box. It takes the window.dialogArguments variable (which originated from the Java applet) and puts this string into a text field. When the user clicks the "click me" button on this page, the window.returnValue is set to the value of this text field. The above HTML then displays the return value using the JavaScript alert() method.

   <HTML>
   <HEAD>
   <TITLE>DialogTestDialog</TITLE>

   <SCRIPT LANGUAGE="JavaScript">
   <!--
   function doOK()
   {
      window.returnValue=document.all.inp1.value;
      window.close()
   }

   function init()
   {
      document.all.inp1.value=window.dialogArguments;
   }

   window.onload=init;

   -->
   </SCRIPT>

   </HEAD>
   <BODY>

   <INPUT TYPE=BUTTON NAME=b1 VALUE="click me" onclick="doOK()">
   <INPUT NAME=inp1 SIZE=30>

   </BODY>
   </HTML>

REFERENCES

See the Internet Client SDK documentation for more information on the showModalDialog method in Internet Explorer 4. Please see Knowledge Base article Q172202 (INFO: Implementing Java Automation Objects using AutoIDispatch) for more information on interactivity between Java and scripting using COM.

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/


Additional query words: modal dialog internet explorer

Keywords : kbcode JCOM JVM
Technology : kbInetDev internet
Version : WINDOWS:2.0,2.01,4.0,4.01
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.