HOWTO: Creating a Modal Dialog Box from Java

ID: Q177753


The information in this article applies to:


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.0.

Please see following article in the Microsoft Knowledge Base for more information on interactivity between Java and scripting using COM:

Q172202 INFO: Implementing Java Automation Objects using AutoIDispatch

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, please see the following pages 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 kbSDKJava300 kbSDKJava310 JCOM JVM kbSDKJava320 
Version           : WINDOWS:2.0,2.01,2.02,3.0,3.1,3.2,4.0,4.01
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 13, 1999