Simple UI
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features,
written in Java. BeanShell is a natural scripting language for Java.
This requires the library bsh-2.0b4.jar to be in classpath.
The following example shows developing simple UI in beanshell script.
// Open input popup dialog
strName = JOptionPane.showInputDialog ( null, " Enter your name : " ) ;
//implementing ActionListener interface (this - context reference)
actionPerformed ( event ) {
print ( event ) ;
}
//implementing WindowListener interface
WindowListener listener = new WindowAdapter () {
public void windowClosing ( WindowEvent w ) {
System.exit ( 0 ) ;
}
} ;
//Create button and add to frame
button = new JButton ( "Click Me" ) ;
button.addActionListener ( this ) ;
frame = new JFrame ( "Hello " + strName ) ;
frame.getContentPane () .add ( button ) ;
frame.addWindowListener ( listener ) ;
frame.setSize ( 200 , 200 ) ;
frame.setVisible ( true ) ;
package com.bethecoder.tutorials.bean_shell;
import java.io.InputStreamReader;
import bsh.EvalError;
import bsh.Interpreter;
public class UITest {
/**
* @param args
* @throws EvalError
*/
public static void main ( String [] args ) throws EvalError {
Interpreter interpreter = new Interpreter () ;
InputStreamReader reader = new InputStreamReader (
UITest. class .getClassLoader ()
.getResourceAsStream ( "simpleui.bsh" )) ;
interpreter.eval ( reader ) ;
}
}