You are here

Get your groove on

Amr M. Kamel's blog - Sat, 11/08/2007 - 5:16am

It is my first time to play with groovy the new scripting language that integrates smoothly with Java platform(s). Although I have heard about it months ago but today I have decided to give it a try.. But I must say I am really amazed, and I don't regret talking that decision at all actually I am grateful :) .

I will just show you an example of how easy it is "especially in building GUI prototypes using Swing", but first let me show you a screen shot of the GUI and if you are a Java developer and you have developed Swing applications before, please take a look at it and think how many lines of code needed to build this GUI beside adding event listeners to all its components, Interfaces you are going to implement ...etc, OK?

I guess you said minimum 30-40 lines of code!! am I right? Well, guess what you can do this in just 16 lines of code :D. Here is the code that made this miracle:

import java.awt.BorderLayout
swing = new groovy.swing.SwingBuilder()
myFrame = swing.frame(title: 'Sample Frame', location:[100,100], size:[300,200]) {
menuBar {
menu(text: 'File') {
menuItem(text: 'Exit', actionPerformed:{System.exit(0)})
} }
panel(layout: new BorderLayout()) {
label(text: 'Input:', constraints: BorderLayout.WEST,
toolTipText: 'Tool Tip')
textField(constraints: BorderLayout.CENTER)
button(text: 'Hello', constraints: BorderLayout.SOUTH,
myFrame.visible = true

More to come ...