Controlling Java from Javascript

Mona M. Everett, Ph.D.

everett@txdirect.net


Writing to the Java Console
Before you try out these techniques, open the Java console. You can find it under the OPTIONS menu in Netscape. When you have it open, click on the clear button to remove its default message. One thing to note is that the console is NOT a \"stay on top \" window. It will immediately disappear behind any other window on which you click.

ConsoleTest
This code is straight from Netscape's example. Below are a button and a text field. Type whatever you want into the text field and click the button labeled "Write". Find the console, which is likely now under Netscape, and you will see that your message has been printed to the console.


The console contents are persistent; if you close and open the console, your text will still be there. Unfortunately, the console does not act as an input device; you cannot read it. The "Write" button contains an onclick routine,

which calls this function:


For our next example, let's do something a little more sophisticated and, in the process, we will use the new split method of the string object. The split method has the following syntax:

Note that the method constructs the new array for you. You can find the length with

We will also compare the ease of debugging with alert boxes, which up to now have been your only choice, and console debugging.
In a Netscape 2.0 page, we calculate a mean and standard deviation of some values. Each has to be entered into an input box, which means that the number of values one can input is limited by the number of input boxes. In this case, we will simply provide a text area and a place to provide the separator which you will use. The example uses a comma as a separator but you can experiment with different ones. The input controls are followed by four buttons:

See Array
Shows you the array it made from the string
Debug with Alert Boxes
Debugs the printarray function with alert boxes
Debug with Console
Debugs the printarray function with console output
Calculate
Calculates the statistics and prints them to the console

Separator Character: Debug On:









Now we will explore the ability of Netscape to directly modify the parameters of an embedded Java Applet. In a separate page, a technique was presented which accomplished applet parameterization on the fly by writing the applet and its parameters into a newly created window. That technique will work for Netscape 2.02 or Netscape 3.0 and above. Of course, direct applet parameterization will only work for Netscape 3.0 and above


This section uses a simple applet developed by Morphic Molecules. It just presents a single line of text in a pleasant setting. It does not scroll, blink, bounce or fade. While these special effects might seem nifty, they are really annoying on a fast computer where text scrolls, blinks, bounces, or fades so fast that the use cannot read it. Unlike most applets, its parameters are addressable and, therefore, accessible to Javascript.
In order to use the applet, download the class file and place it in the same directory as your html file (or set the code base parameter to where you are going to put it). You can also download the source, which could have a number of enhancements made to it. Get both the class and the source along with a short HTML file in a zip file if you wish. If you use Symantec's Cafe to build Java applets, download a zip file with the complete project files.
This applet has ten variables which have public get and set methods. Set methods take a single string parameter and get methods return a single string parameter.
Variables passed via applet parameters and get/set methods are error checked. Variable set by direct access by Javascript are NOT checked at all. Be careful! Further, some variable types cannot be directly passed, such as colors, where a string representation of a color must be laboriously converted into a color object. Consequently, the variables are private but accessible with get and set methods.
The following table describes these variables, their uses and the methods available to get and set them.

Public variables of the MessageBox class

Property

Type

Permissible Values

Notes

vmargin


applet.setVMargin(string)
string = applet.getVMargin()

integer any integer Values should be small; Will be used for both top and bottom margins. Defaults to 10

hmargin


applet.setHMargin(string)
string = applet.getHMargin()

integer any integer Values should be small; Will be used for both top and bottom margins. Defaults to 10

borderwidth


applet.setBorderWidth(string)
string = applet.getBorderWidth()

integer any integer Values should be small. Will be used to determine width of border around the applet. Defaults to 1. Set to 0 to specify no border.

theFontName


applet.setFontName(string)
string = applet.getFontName()

string Can only be Courier, Helvetica, and Roman Font that message is printed in. Defaults to Courier.

theFontStyle


applet.setFontStyle(string)
string = applet.getFontStyle()

string Can only be plain, bold, italic, and bolditalic Style that message is printed in. Defaults to bold.

theFontSize


applet.setFontSize(string)
string = applet.getFontSize()

integer any legitimate font size Defaults to 14

theMsg


applet.setMsg(string)
string = applet.getMsg()

string Message you want presented Applet will clip your message if it does not fit.

justification


applet.setJustification(string)
string = applet.getJustification()

string Allowed values are left, right, and center Justification of Message Allowed values ; Defaults to center

theFontColor


applet.setFontColor(string)
string = applet.getFontColor()

string Allowed values are red, green,blue,pink, orange, magenta,cyan,white, yellow, gray, and black Message will be drawn in this color. Defaults to blue.

theBGColor


applet.setBGColor(string)
string = applet.getBGColor()

string Allowed values are red, green,blue,pink, orange, magenta,cyan,white, yellow, gray, and black Background rectangel, determined by vmargin and hmargin, will be drawn in this color. Defaults to yellow

theBorderColor


applet.setBorderColor(string)
string = applet.getBorderColor()

string Allowed values are red, green,blue,pink, orange, magenta,cyan,white, yellow, gray, lightgray, darkgray,and black Border will be drawn in this color. Defaults to blue

theInset
applet.setInset(string)
string = applet.getInset()

string Allowed values are none, raised, and lowered. Empty string also causes none. Defaults to none.

Notes on Execution
Font size
If you set the font size by typing in the edit box, it will not happen until you click on something else which can receive focus.


Known bugs
Margins
Margins, especially the vertical margin, do not set correctly.
Clipping
The applet does not clip the message. If you make the font too big, it will overrun the intended area




Margins Test
Vertical   Horizontal   Border width  
Colors
Font Color   Background Color   Border Color
Font
Font Face    Font Style    Font Size
Other
Justification    Box Inset
Message
Message




This page was last updated on


Mail comments about Morphic Molecules pages to everett@txdirect.net



CENTER" WIDTH="80%" SIZE="5">