This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes javascript to applet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "javascript to applet communication" Watch "javascript to applet communication" New topic
Author

javascript to applet communication

Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Hi,
I need some help and I hope someone here can lend me a hand. I'm trying to communicate my javascript code with a java applet. I can do this in a very basic way but not in the more complex way I am trying now. What I would like to do specifically is use java as an error checking tool on my web pages. If a user enters invalid data, or I need to tell them something, I want to popup a java dialog box. I want to use Java for several reasons, the biggest of which is with Java I will have control over the dialog boxes I create, ie., the font size, text input box size, and so forth. I do not have control over these features with the javascript alert() boxes. And since the dialog boxes have to be MODAL, Java will also help me with this feature. If someone can please give me a hand and look at my code I would really appreciate it. I thought I read somewhere that javascript cannot init() classes or create "new()" instances of classes. Could this be my problem? Please help if you can. Keep in mind that the applet does not really nor will it communicate with the javascript as it may appear. This was just an attempt to put something in the applet and html.
// dialog.html which holds the applet
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>HTML to APPLET DIALOG BOX COMMUNICATION</title>
<script language="JavaScript1.2">
function sendToApplet()
{
// here is my call to the java applet function
document.gui.gui();
}
</script>
</head>
<body>

<center>
<APPLET
CODE = "gui"
NAME = "gui"
CODEBASE = "."
WIDTH = 100
HEIGHT = 100
HSPACE = 0
VSPACE = 0
ALIGN = middle>
</APPLET>
</center>
<form name="appletform" id="appletform" method="POST">
COM 1:
<input name="lblcom1" id="lblcom1" align="center" size="40" value="Contacting COM 2 ...">
<input type="Button" value="send" name="txtcom1" id="txtcom1" onclick="sendToApplet();">
</form>
</body>
</html>
// gui.java which is my applet code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import netscape.javascript.JSObject;
//import netscape.javascript.*;
public class gui extends Applet implements ActionListener
{
Button showbutton, scrambutton, framebutton;
alert a;
popupframe pf;
public void init()
{
showbutton = new Button("Show dialog");
add(showbutton);
showbutton.addActionListener(this);
scrambutton = new Button("Scram dialog");
add(scrambutton);
scrambutton.addActionListener(this);
framebutton = new Button("Popup Frame");
add(framebutton);
framebutton.addActionListener(this);
}

public void gui()
{
Container c = this.getParent();
if (c != null)
{
if (c instanceof Frame)
{
a = new alert((Frame)c, "Are you sure you want to start global thermal nuclear war?");
a.show();
}
}
}
public void actionPerformed(ActionEvent evt)
{
Container c = this.getParent();
if (evt.getSource() == showbutton)
{
gui();
}
if (evt.getSource() == scrambutton)
{
c = null;
a.hide();
}
if (evt.getSource() == framebutton)
{
pf = new popupframe("Popup Frame");
pf.show();
}
}
}
class alert extends Dialog implements ActionListener
{
Button cmdFire, cmdJoekastan;
TextField txtJoekastan;
Label lblWarn, lblJoekastan, spacer1, spacer2;
String emptyStr = "";
popupframe pf;// same as: buttondialog dialogBox;
alert(Frame browserframe, String s)
{
super(browserframe, true);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
//constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;

// BEGIN ROW 1
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 3;
constraints.gridheight = 1;
lblWarn = new Label(s);
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblWarn, constraints);
add(lblWarn);

// BEGIN ROW 2
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 3;
constraints.gridheight = 1;
spacer1 = new Label(emptyStr);
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(spacer1, constraints);
add(spacer1);
// BEGIN ROW 3
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
lblJoekastan = new Label("COM 2: ");
gridbag.setConstraints(lblJoekastan, constraints);
add(lblJoekastan);

constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.gridheight = 1;
txtJoekastan = new TextField(10);
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(txtJoekastan, constraints);
add(txtJoekastan);

// BEGIN ROW 4
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 3;
constraints.gridheight = 1;
spacer2 = new Label(emptyStr);
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(spacer2, constraints);
add(spacer2);

// BEGIN ROW 5
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 3;
constraints.gridheight = 1;
cmdJoekastan = new Button("Send");
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(cmdJoekastan, constraints);
add(cmdJoekastan);
cmdJoekastan.addActionListener(this);

constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 3;
constraints.gridheight = 1;
cmdFire = new Button("FIRE");
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(cmdFire, constraints);
add(cmdFire);
cmdFire.addActionListener(this);

resize(300, 250);
move(200, 200);
}

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == cmdFire)
{
hide();
}
if (evt.getSource() == cmdJoekastan)
{
pf = new popupframe("Joekastan");
pf.show();
}
}
}

class popupframe extends Frame implements ActionListener
{
Button cmdOKPopup;
popupframe(String title)
{
super(title);
resize(100,100);
move(320,240);
cmdOKPopup = new Button("OK");
add(cmdOKPopup);
cmdOKPopup.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{

if (evt.getSource() == cmdOKPopup)
{
hide();
}
}
}
Rakesh Ray
Ranch Hand

Joined: Jul 25, 2001
Posts: 51
What is the question?
Code looks fine.
Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
I guess I did forget to post the actual question I had, DUHHHH. Sorry about that. I got it figured out on my own anyway. I had to declare the applet below the form to make it actually work. The applet code had the know about the form values (names) so it could access them, and it didn't know the form information unless I placed the applet below the form in the html code.
Joe Crew
 
Consider Paul's rocket mass heater.
 
subject: javascript to applet communication
 
Similar Threads
grid back gridx
grid back behaviour
JScrollPane Problem
Gridbag Layout
align JLabels and JTextFields vertically in different areas