F O R · R E A L L Y · T H I N · C L I E N T S
 Home  |  Overview  |  Widgets  |  Demo  |  License  |  History 

This overview is a step by step guide to create a simple calculator applet and application.

See the API documentation, the detailed list of Thinlet's public methods.

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" />
  <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 and invokes its init method. The init method loads the panel (decribed in the xml file) and add to the desktop.
import thinlet.*;

public class Calculator extends Thinlet {
  public Calculator() {
    try {
      add(parse("calculator.xml"));
    } catch (Exception exc) { exc.printStackTrace(); }
  }

  public static void main(String[] args) {
    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() {
    try {
      int i1 = Integer.parseInt(getString(find("number1"), "text"));
      int i2 = Integer.parseInt(getString(find("number2"), "text"));
      setString(find("result"), "text", String.valueOf(i1 + i2));
    } catch (NumberFormatException nfe) {
      getToolkit().beep();
    }
  }

Compile, run, and deploy
Add the thinlet/src directory or the lib/thinlet.jar to your classpath and compile the source using e.g the jikes Calculator.java command, and run it, java Calculator. For applet create a jar archive including Calculator.class, thinlet/*.class, calculator.xml (and later image) files (jar cvfM calculator.jar Calculator.class thinlet/*.class 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.

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" width="320" height="320">
  <param name="class" value="Calculator">
</applet>

Copyright © 2002 Robert Bajzat - info@thinlet.com - All rights reserved.