Logo DynamicJava

Embedding DynamicJava

This section shows you how to embed a DynamicJava interpreter into a JavaTM application.

The following program creates a JFrame and exports it to a DynamicJava interpreter. Then an external script is interpreted in order to initialize the JFrame. After the script has been executed, the application shows the JFrame.
This example illustrates how to customize a JavaTM object from a DynamicJava script.

Here is the source code of the JavaTM application (Embedding.java):

import java.io.*;
import javax.swing.JFrame;

import koala.dynamicjava.interpreter.*;
import koala.dynamicjava.parser.wrapper.*;

public class Embedding {
    public static void main(String[] args) {
	JFrame frame = new JFrame();

	// Create the interpreter. It will use the default JavaCC parser.
	Interpreter interpreter = new TreeInterpreter(new JavaCCParserFactory());

	// Export the JFrame to the interpreter.
	interpreter.defineVariable("frame", frame);
	
	// Interpret the script
	try {
	    interpreter.interpret("init.djava");
	} catch (InterpreterException e) {
	    System.err.println(e);
	} catch (Throwable e) {
	    System.err.println(e);
	}

	// Show the frame
	frame.show();
    }
}
    

Here is the source code of the DynamicJava script (init.djava):

// Customize the size of the frame
frame.setSize(200, 200);

import java.awt.event.*;

// Add a window listener to the frame
frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});
    

This script sets the size of the JFrame and adds a window listener to it. The listener causes the application to exit when the JFrame is closed.


Send comments, suggestions and bug reports to sillion@ilog.fr
Last modified: Mon Nov 22 17:22:02 MET 1999