Creating GUI applications using jrunscript

I have always been looking forward to a scripting language for creating and playing with Java GUI applications easily. jrunscript introduced in JDK6.0 is a powerful command line scripting shell. A.Sundarajan has written very useful blogs on jrunscript http://blogs.sun.com/sundararajan/entry/command_line_script_shell_in
In this blog, my attempt is to give an introduction on writing simple GUI applications using JavaScript.
If you have read the above blog, you would know that a group of commands could be grouped into a script file (just like a bat or shell script file)

Create a simple frame of size 400\*400 with a button added to it:
importPackage(javax.swing);
importPackage(java.lang);

frame = new JFrame("JFrame");
frame.setSize(400, 400);

btn = new JButton("click me");
frame.getContentPane().setLayout(new java.awt.FlowLayout());
frame.getContentPane().add(btn);
frame.setVisible(true);
Thread.sleep(2000);


The code above is pretty straightforward and except for the two lines, the rest are what a developer would ideally write in a simple java program. For details on the jrunscript syntax, refer to the above blog as suggested.
Save the above code into a file (say test.js) and run jrunscript test.js

Create listeners (interfaces) containing single method only

One way is to define a simple javascript function and pass it to the actionListener as below:

btn = new JButton("click me");
function action(e)
{
System.out.println("button clicked..." + e.getID());
}
btn.addActionListener(action);

The argument e here maps to java.awt.event.ActionEvent class.

Second way is to define the exact method name as defined in ActionListener interface in java.awt.event package and create a JavaScript object out of it

btn = new JButton("click me");
listener1 = {
actionPerformed: function (e)
{
System.out.println("ActionPerformed: button clicked..." + e.getID());
}
}

alistener = new ActionListener(listener1);
btn.addActionListener(alistener);


Note above that alistener is the ActionListener object created using listener1 javascript object.


Create listeners(interfaces) containing multiple methods:

Consider another simple example where I would want to trap key events (keypressed, keytyped, keyreleased) in an editable field (JTextField for instance). java.awt.event.KeyListener interface has multiple methods declared inside it.

This is how one could implement it

listener2 = {
keyPressed: function(e)
{
System.out.println("key Pressed..." + e.getID());
},
keyReleased: function(e)
{
System.out.println("key Released..." + e.getID());
},
keyTyped: function(e)
{
System.out.println("key Typed..." + e.getID());
}
}

keylistener = new KeyListener(listener2);
tf = new JTextField("Type here");
tf.addKeyListener(keylistener);

Note above that function definitions are separated by commas inside the listener2 javascript object block.


A simple animation using JDialog:

parent = new JFrame();
dlg = new JDialog(parent, "JDialog(Modaless)", false);
label = new JLabel("This is a cool demo using jrunscript tool...");
dlg.getContentPane().add(label);
x=400;
dlg.setLocation(x, 200);
dlg.setVisible(true);
var size = dlg.getWidth();
expand = true; //expand dialog
while (true) {
if (expand && size < 400) {
dlg.setLocation(x, 200);
dlg.setSize(size, 50);
size = size + 6;
x = x - 3;
Thread.sleep(20);
continue;
}
expand= false; //shrink dialog
if (size > 0) {
dlg.setLocation(x, 200);
dlg.setSize(size, 50);
size = size - 6;
x = x + 3;
Thread.sleep(20);
continue;
}
expand = true;
}

The code above when run would expand and shrink a dialog with a time delay of 20ms. The exact degree of smoothness may depend on the system's configuration but the above code should give you an idea of what is being tried out here.


A multitask application combining all the above code along with another JList where items are selected by rotation

Here is the complete runnable code;


importPackage(javax.swing);
importPackage(java.lang);
importPackage(java.awt.event);

frame = new JFrame("JFrame");
frame.setSize(400, 400);

btn = new JButton("click me");
function action(e)
{
System.out.println("button clicked..." + e.getID());
}
listener1 = {
actionPerformed: function (e)
{
System.out.println("ActionPerformed: button clicked..." + e.getID());
}
}

alistener = new ActionListener(listener1);
btn.addActionListener(alistener);

listener2 = {
keyPressed: function(e)
{
System.out.println("key Pressed..." + e.getID());
},
keyReleased: function(e)
{
System.out.println("key Released..." + e.getID());
},
keyTyped: function(e)
{
System.out.println("key Typed..." + e.getID());
}
}


keylistener = new KeyListener(listener2);

tf = new JTextField("Type here");
tf.addKeyListener(keylistener);
frame.getContentPane().setLayout(new java.awt.FlowLayout());
frame.getContentPane().add(tf);
frame.getContentPane().add(btn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

frame1 = new JFrame("JList...");
strs = java.lang.reflect.Array.newInstance(java.lang.String, 5);
strs[0] = "First";
strs[1] = "Second";
strs[2] = "Third";
strs[3] = "Fourth";
strs[4] = "Fifth";
list = new JList(strs);
frame1.getContentPane().add(list);
frame1.setLocation(500, 0);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(400, 400);
frame1.setVisible(true);

runit = {
run: function() {
index = 0;
while (true) {
if (index > 4) index = 0;
list.setSelectedIndex(index);
index++;
Thread.sleep(200);
}
}
}
t = new Thread(Runnable(runit));
t.start();


parent = new JFrame();
dlg = new JDialog(parent, "JDialog(Modaless)", false);
label = new JLabel("This is a cool demo using jrunscript tool...");
dlg.getContentPane().add(label);
x=400;
dlg.setLocation(x, 200);
dlg.setVisible(true);
var size = dlg.getWidth();
expand = true; //expand dialog
while (true) {
if (expand && size < 400) {
dlg.setLocation(x, 200);
dlg.setSize(size, 50);
size = size + 6;
x = x - 3;
Thread.sleep(20);
continue;
}
expand= false; //shrink dialog
if (size > 0) {
dlg.setLocation(x, 200);
dlg.setSize(size, 50);
size = size - 6;
x = x + 3;
Thread.sleep(20);
continue;
}
expand = true;
}
Thread.sleep(2000);


Hopefully the above code should run fine for everyone :-)

This should perhaps help GUI beginners to get started writing simple GUI programs and gradually continue playing with the powerful APIs offered by java GUI.

Any questions or comments are most welcome

Thanks,
Kannan
Comments:

Nice one Kannan ! I remeber the funny example made by the guy in Sun Tech Days. A button with the caption "Press Me" and after clicking " Dont Press me again" :) These examples are very helpful.

Posted by Vaibhav Choudhary on March 05, 2007 at 03:37 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

kannan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today