Layout and component properties
Edit an xml file (called calculator.xml) which describes the
hierarchy and the static attributes of the components. The root panel
component contains 3 textfields, a label (+) and a button (=).
For event handling identify the textfields with names, and
add method name (action listener) for the button.
<panel gap="4" top="4" left="4">
<textfield name="number1" columns="4" />
<label text="+" />
<textfield name="number2" columns="4" />
<button text="=" action="calculate(number1.text, number2.text, result)" />
<textfield name="result" editable="false" />
</panel>
Java source both for applet and application
Create the Calculator class (Calculator.java) extending Thinlet.
Running the calculator as application the main method creates a frame
including the Calculator component.
The constructor loads the panel (decribed in the xml file) and add to
the desktop.
package thinlet.demo;
import thinlet.*;
public class Calculator extends Thinlet {
public Calculator() throws Exception {
add(parse("calculator.xml"));
}
public static void main(String[] args) throws Exception {
new FrameLauncher("Calculator", new Calculator(), 320, 240);
}
}
Event handling
Complement the Calculator.java source with the business logic.
The calculate method computes the sum of the first two fields' value, and
updates the third field.
public void calculate(String number1, String number2, Object result) {
try {
int i1 = Integer.parseInt(number1);
int i2 = Integer.parseInt(number2);
setString(result, "text", String.valueOf(i1 + i2));
} catch (NumberFormatException nfe) {
getToolkit().beep();
}
}
Compile, run, and deploy
Note: the -target 1.1 and -source 1.3 options
are required if you developed your application for 1.1 compatible Java VMs
(e.g. IE's default VM). Only the 1.1 subset of the API is available for these
applications, e.g. to check you application download the JDK 1.1 version.
Compile the source in the thinlet/src folder using
e.g. the javac -target 1.1 -source 1.3 thinlet/demo/Calculator.java
command, and run it, java thinlet.demo.Calculator.
For applet compile the applet launcher (javac -target 1.1 -source 1.3
thinlet/AppletLauncher.java) and create a jar archive including
thinlet/demo/Calculator.class, thinlet/*.class,
thinlet/demo/calculator.xml
(and later image) files (jar cvfM calculator.jar thinlet/demo/Calculator.class
thinlet/*.class thinlet/demo/calculator.xml). Note, Netscape 4.x browser doesn't
load unknown (e.g. *.xml) resources from jar archive, thus rename the
files to calculator.txt and update the java sources.
To decrease the class size use the -g:none compiler option
(especially for thinlet/Thinlet.java).
Use thinlet.AppletLauncher to load any thinlet component, you have to add
the class parameter including the class name of your thinlet component.
<applet code="thinlet.AppletLauncher" archive="calculator.jar" width="320" height="200">
<param name="class" value="thinlet.demo.Calculator" />
</applet>