File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Controlling applet from html code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Controlling applet from html code" Watch "Controlling applet from html code" New topic

Controlling applet from html code

Rajeshwari Ramamurthi

Joined: Jun 21, 2001
Posts: 28
Is it possible to change the contents of the applet from html page where it is embedded
I have a network diagram. I have to change the contents of applet, depending on where the user clicks on the diagram.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 829
Im new at this but by "changing the contents of the applet" do you mean insert a different applet in it's place or just change variable(s) values? I would guess by using Javascript and accessing the applet array for the page you could do just about anything you want. I mainly program in VB, Perl, etc.., and have only recently started looking at Java so I hope my answers and questions in here arent dumb.
Darren Hicks

Joined: Jul 31, 2001
Posts: 6
Yes, you can have HTML (via Javascript )
communicate with an Applet.
Here are the basics:
1) Define a public method in your Applet that other script can access ( I used setMessage )
2) Define a Javascript function to delegate a call to the Applet, accessing the applet via document.applets['TestHtmlCommApplet'].
3) Call the javascript in the appropriate places ( and with parameters, if necessary ). I'm calling it from the onclick event of a button.
Here's code that will work, so long as the .java is compiled and in the same directory as the html.
---------------- --------
import javax.swing.*;
public class TestHtmlCommApplet extends JApplet
private String msg;
public void init() {}
public void setMessage( String disp )
msg = disp;
getContentPane().setVisible( false );
getContentPane().add( new JLabel( disp, JLabel.CENTER ) );
getContentPane().setVisible( true );
----------- TestHtmlCommApplet.html -------------
<title>HTML/Applet Test</title>
<script language="JavaScript1.2">
// changeDisplay will dispatch a call to the Java Applet
function changeDisplay( )
// The applet has a method called setMessage
document.applets['TestHtmlCommApplet'].setMessage( document.forms[0].display.value );
<body bgcolor=#C0C0C0 topmargin="2" leftmargin="2" bottommargin="2" rightmargin="2" >
<H1>HTML Communicating to an Applet</H1>
<applet codebase="." applet code="TestHtmlCommApplet"
name="TestHtmlCommApplet" width=300 height=100>
<INPUT TYPE=TEXT NAME=display VALUE="Show in applet..."><BR>
<INPUT TYPE=BUTTON VALUE="Send Text To Applet" nclick="changeDisplay();">
Sorry if the formatting translates poorly to the message board
( NOTE: I could not post with "onclick" in HTML above, change "nclick" to "onclick"! )
You can go the other direction, from Applet to Javascript on an HTML page by including the MAYSCRIPT tag in your HTML <Applet> tag.

&&&&&&& D<br />@@ @ @@ A<br /># ### # R<br />$$ $ $$ R<br />! !!! ! E<br />....... N
I agree. Here's the link:
subject: Controlling applet from html code