User Tools

Site Tools


tutorial:usejava

Using Java from Scripts

It's possible to use SyMAT code from scripts, as well as access Java classes. This isn't a tutorial per se; rather, a working example program written in JavaScript. It is intended as a starting point for writing plugins with their own user interfaces.

SyMAT 2.1 and above includes a Java interpreter, so you can write pure Java code instead.

scriptingjava.syjs
importPackage(Packages.javax.swing);
importPackage(java.awt.event);
importPackage(Packages.net.apocalypselabs.symat);
 
function init() {
	var frame = new JInternalFrame("Test");
	frame.setClosable(true);
	frame.setLayout(null);
	frame.setSize(300,300);
	var btn = new JButton("Hi");
	frame.add(btn);
	btn.setLocation(5,10);
	btn.setSize(100,30);
	var btnaction = { actionPerformed: btnAction };
	btn.addActionListener(btnaction);
	Main.loadFrame(frame);
}
 
function btnAction() {
	notify("Button pressed!");
}
 
init();

This code first imports some Java classes, including SyMAT's main codebase. In the init() function, we make a new JInternalFrame with the title “Test”, and set some properties. Then we make a button and add it to the frame. We set the button size and location. Then we make a new ActionListener and add it to the button, so when the button is clicked the code inside btnAction() will run. Then we access SyMAT's window layout function and tell it to display the frame we made. The last line of the code calls the init() function, which makes things happen.

tutorial/usejava.txt · Last modified: 2019/04/16 02:40 (external edit)