GroovySWT is a wrapper around SWT, the eclipse Standard Widget Toolkit. It allows you to easily write Eclipse SWT applications by using Groovy's builder mechanism.
Here is some SWT code using native Groovy:
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
import org.eclipse.swt.layout.RowLayout as Layout
def display = new Display()
def shell = new Shell(display)
shell.layout = new Layout(SWT.VERTICAL)
shell.text = 'Groovy / SWT Test'
def label = new Label(shell, SWT.NONE)
label.text = 'Simple demo of Groovy and SWT'
shell.defaultButton = new Button(shell, SWT.PUSH)
shell.defaultButton.text = ' Push Me '
shell.pack()
shell.open()
while (!shell.disposed) {
if (!shell.display.readAndDispatch()) shell.display.sleep()
}
When you run this (see below for setup details), the result looks like:

Here is the same example using SwtBuilder - note that this example does more because it actually prints some text 'Hello' to standard output when you press the button:
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
def swt = new groovy.swt.SwtBuilder()
def shell = swt.shell(text:'Groovy / SWT Test') {
rowLayout()
label(style:"none", text:'Simple demo of Groovy and SWT')
button(style:"push", text:' Push Me ') {
onEvent(type:"Selection", closure:{ println "Hello" })
}
}
shell.pack()
shell.open()
while (!shell.disposed) {
if (!shell.display.readAndDispatch()) shell.display.sleep()
}
Here is another example which explicitly creats a script class and assumes guiBuilder is passed in via a binding:
package com.dacelo.guidedsales.ui
class About extends Script {
run( ) {
def subapp = guiBuilder.shell( parent ) {
gridLayout()
group( text:"Groovy SWT", background:[255, 255, 255] ) {
gridLayout()
label( text:"groove fun !" ,background:[255, 255, 255] )
label( text:"Email: ckl@dacelo.nl", background:[255, 255, 255] )
}
}
subapp.pack()
subapp.open()
}
}
Examples
For a short example of using the JFace builder for creating a standard application see the TextEditor sample.
For a comparison of the groovy and java code see here or see the latest code in subversion.
Further information
The sources (and README.txt telling you how to set up the libraries and dll's) can be found in subversion:
The jar file compiled against Eclipse SDK 3.3 jars can be found attached:
To run groov-swt application you also need some of the eclipse libraries (including SWT), so download groovy-swt-0.3-including-Eclipse-libs.ZIP and unzip all the jar files into the lib directory of your groovy installation. This zip file includes the windows version of SWT, so if you are on another platform you need to download the appropriate version of SWT at eclipse and include that instead.
I am making another implementaion of GroovySwt. It has some features mentioned below.
creating program and the program makes all factory's source code .
button(text:"hello world" , background:[100 , 100, 100] , SWT.PUSH)
this code is same as below.
Button btn = new Button(parent , SWT.PUSH) ;
btn.setText("hello world") ;
btn.setBackground(new Color(btn.getDisplay() , 100 , 100 , 100)) ;
( note: The Color resource is managed automatically . )
If Construction arguments' length is longer than 1 , just wrap those in list.
tabItem(text:"hello world" , [SWT.NONE , 1])
custom implementation classes.
button( SWT.PUSH ) {
selectionListener(
widgetSelected:{ println "hello world" /* define inline */ }
)
}
// custom implementation of SelectionListener
CustomSelectionListener listener = new CustomSelectionListener() ;
button( SWT.PUSH ) {
selectionListener(listener) // use custom class
}
you can use 'BUTTON' to use org.eclipse.swt.widgets.Button . This mapping
is configured by configuration file at runtime .
(note : The default mapping (class name) is always usable.)
My Implementation is not perfect currently , but it works fine.
I want to contribute my works , is It possible ?
Yes, if you have some useful code to contribute, that would be great.
Hello,
I get SWT.zip, but i don't know how to install . I've read some webs, but i haven't found nothing about this.
Could someone help me about this? Im working on Windows XP. I've installed Groovy and Eclipse.
Thanks!
Download the attachment called groovy-swt-0.3-including-Eclipse-libs.ZIP and unzip everything into you groovy/lib directory and remove the old groovyswt.jar from that directory.
Frank
The link for groovy-swt-0.3-including-Eclipse-libs.ZIP is broken. Can someone fix it?
The link for groovy-swt-0.3-including-Eclipse-libs.ZIP should now be working again.
Frank