Microsoft KB Archive/177753

= How To Creating a Modal Dialog Box from Java =

Article ID: 177753

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 2.01
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 3.0
 * Microsoft Software Development Kit for Java 3.1
 * Microsoft Software Development Kit for Java 3.2
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.0 128-Bit Edition

-



This article was previously published under Q177753



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.   Dialog Test    



   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.   DialogTestDialog</TITLE>

<SCRIPT LANGUAGE="JavaScript">

</SCRIPT>

</HEAD> 

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

</BODY> </HTML>

<div class="references_section">