0u93grs9r5shpm541ge8v424j7
Dialogs in Groovy
TechArea - Java
Dienstag, 16. Juni 2009 um 15:03

GUI Dialogs in Groovy

One of the major issues in GUI dialog programming is how to fetch the values a user entered. Since the groovy documentation is a little short about this matter, I give an introduction on how this can be achieved.

We provide a dialog with a text field, a checkbox, a pair of radio buttons, a combo box, and two termination buttons. The code is:


import groovy.swing.SwingBuilder
import java.awt.FlowLayout as FL
import javax.swing.BoxLayout as BXL

def s = new SwingBuilder()
s.setVariable('myDialog-properties',[:]) //-- 1 --//
def vars = s.variables //-- 2 --//
def dial = s.dialog(title:'Dialog 1',id:'myDialog',modal:true) { //-- 3 --//
    panel() {
        boxLayout(axis:BXL.Y_AXIS)
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Name')
            textField(id:'name',columns:10) //-- 4 --//
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            checkBox(id:'developper',text:'Developper')
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Gender:')
            myGroup = buttonGroup()
            radioButton(id:'genderMale', text:"male",
                        buttonGroup:myGroup, selected:true)
            vstrut(height:12)
            radioButton(id:'genderFemale', text:"female",
                        buttonGroup:myGroup)
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Country')
            comboBox(id:'country',items:['luna','calypso'])
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            button('OK',preferredSize:[80,24],
                   actionPerformed:{
                       vars.dialogResult = 'OK' //-- 5 --//
                       dispose()
            })
            button('Cancel',preferredSize:[80,24],
                   actionPerformed:{
                       vars.dialogResult = 'cancel'
                       dispose()
            })
        }
    }
}
dial.pack()
dial.show()

println 'and the result is: ' + vars.dialogResult
println 'the name entered is: ' + vars.name.text
println 'gender: ' + (vars.genderMale.selected ? 'male' : 'female')
println 'developper: ' + vars.developper.selected
println 'country: ' + vars.country.selectedItem

Just put this in the groovyConsole and run it with Ctrl+ENTER.

Remarks only for dialog handling (the rest is Groovy standard, see Groovy docs):

At //--1--// and //--2--// we define a variable inside the SwingBuilder object and a code accessible variable holding all SwingBuilder variables. This is very handy, you can put all sorts of stuff in there from anywhere, especially inside the action handlers, and you can access it after the dialog has finished.

At //--3--// we just define this to be a modal dialog (so dial.show() will wait until the dialog is finished).

At //--4--// we have the first value field, all fields in the example have an ID set, so at the end we can access all fields by simply typing vars.THE_FIELD_ID - the way of obtaining the value depends on the the type of the field, see the println statements at the end of the listing.

At //--5--// we set a new variable telling how we left the dialog. We could also have used s.th. like


        ...
        vars.'myDialog-properties'.result = 'OK'
        ...
    println 'and the result is: ' + vars.'myDialog-properties'.result
    ...

here, both ways are equally valid.



There is a different way to handle actions, we could also introduce an action closure; so add a


def okAction = s.action(name:'OK',closure: {
    vars.dialogResult = 'OK'
    dispose()
})

somewhere after the definition of s, and use

      ...
      button(action:okAction)
      ...

to connect the action to the buttons (the button text is automgically taken from the action name).

From the introduction given here you should be able to construct dialogs of any size in the Groovy way, i.e. with just stuff, no fluff. HAPPY CODING!

(the code is provided as-is, under no circumstances shall the site owner be made responsible for the working abilities; the code is distributed under the LGPLv3)

 
Powered by Joomla!.   (c) P. Späth 2008