Article ID: 177753
Article Last Modified on 6/14/2006
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>
172202 INFO: Implementing Java Automation Objects using AutoIDispatch
Additional query words: modal dialog internet explorer
Keywords: kbhowto kbcode KB177753