Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

How Griffon Helps MigLayout

  • submit to reddit

Coding the Controller

Next, since our "Search" button currently does nothing, we need to think about where to move our original ActionListener.

Here's a nice rule of thumb: Anything that controls anything belongs in the controller.

Let's put it in the controller as follows, very reminiscent of the code with which this article started (note that this is why you needed 'id:searchField' and 'id:resultArea' as properties in the JTextField and JTextArea in your view, to make things such as 'view.searchField.text' in the the code below possible):

class ShakesWSClientController {

    // these will be injected by Griffon
    def model
    def view

    def searchAction = { evt ->

        def xml = model.proxy.GetSpeech(view.searchField.text)

        def XmlParser parser = new XmlParser()

        def speech = parser.parseText (xml)

        view.resultArea.text =  ["PLAY: ${speech.PLAY.text()}\n",
         "SPEAKER: ${speech.SPEAKER.text()}\n",
         "TEXT: ${speech.text()}"].sum("")


The model needs to change and so does the Startup.groovy file. Why? Because, initially, we had hard coded the search string in the Startup.groovy file and then bound the domain object to it. Instead of that, we'll simply define our proxy as the domain object, without needing to bind it to anything since it isn't going to change:

import groovy.beans.Bindable

class ShakesWSClientModel {

def proxy


Now our Startup.groovy is simply as follows:


def rootModel = app.models.root

rootModel.proxy = new WSClient

That's it. Our controller is done and we've tweaked our model so that we're able to perform our action event in the controller. Next, all we need to do is hook our action into our button. Remember how our button simple had the "Search" text in it, together with two constraints? Simply replace the text with "action: searchAction" and type "build(ShakesWSClientActions)" below the view's import statement. Then create a file (the first file you've had to create manually) named 'ShakesWSClientActions' and add this content:

actions {

action( id: 'searchAction',
name: "Search",
closure: controller.searchAction,
accelerator: shortcut('S'),
mnemonic: 'S',
shortDescription: "Looking for Shakespeare"


Your application, now all in Groovy, using MigLayout for the UI design, with the sources structured very rigidly according to the Griffon framework... is complete. Run it and you'll have the same result as before:


This article has provided a brief introduction to MigLayout, both in the context of Java and in the context of Griffon. The advantages of Groovy over Java have been pointed out. The coolness of MigLayout has been demonstrated. The integration of a Groovy web service has also been shown, within a Java/Groovy application, as well as within a Griffon application.

Postscript: Interested in creating the samples discussed here yourself? In addition to the JDK and Griffon, you'll also need ant.jar on your classpath together with groovyws-standalone.jar (currently groovyws-standalone-0.3.1.jar) and miglayout-swing.jar (currently miglayout-3.6-swing.jar).


fig-2.png12.55 KB
fig-1.png32.25 KB
fig-3.png11.97 KB
fig-4.png55.82 KB
Published at DZone with permission of its author, Geertjan Wielenga.