Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Controlling applet from html code

Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 837
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
It's exactly the same and completely different as this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic