X

Geertjan's Blog

  • March 29, 2007

How Cool is Swing in Groovy?

Geertjan Wielenga
Product Manager
Let's say you wanted to create this GUI:

If you were to sketch this out quite roughly in pseudo code, the content and layout would basically be this:

frame (title:'Demo') {
menuBar {
menu('File') {
menuItem 'New'
menuItem 'Open'
}
}
panel {
label 'Label 1'
slider()
comboBox(items:['one','two','three'])
}
}
frame.show()

Anyone, regardless of their insight into programming, would be able to come up with something like the above. Well, how cool is Groovy? You can literally paste the above into a Groovy context, add a few things to load the SwingBuilder library and pack the frame, and you're done:

When I run the above from the IDE, I get the GUI shown at the start of this blog entry. Given how easy this all is in Groovy, I wonder whether the Groovy community even needs a Matisse-like GUI Builder. It all seems pretty intuitive, and with syntax coloring and code completion, plus maybe one or two other fun things (such as the Navigator shown above), coding Swing in Groovy is going to be a breeze.

By the way, the listing above comes directly from section 8.5 of "Groovy in Action". The section is called "Easy GUIs with SwingBuilder" and, based on my first cursory explorations, that title delivers on its promise. (A second "by the way" is that everything you see in the screenshot above,i.e., this support in NetBeans IDE 6.0 for Groovy, is freely available, if you follow the instructions in my blog entry from yesterday. Currently, it is buggier than the underside of a rock in a desert, but that's why it is a development version.)

Join the discussion

Comments ( 12 )
  • Danno Ferrin: ... Speling Errors Thursday, March 29, 2007
    [Trackback] Geertjans been doing great things with Groovy and NetBeans. Given how easy this all is in Groovy, I wonder whether the Groovy community even needs a Matisse-like GUI Builder. It all seems pretty intuitive, and with syntax coloring and code...
  • Mike A Thursday, March 29, 2007
    This is all well and great, but this is really a very trivial example to draw any serious conclusion from. I don't know Groovy, or SwingBuilder for that matter, but for instance how do you wire actions, and events? How can you use external resources for menus, actions, icons, and such? How do you do data binding? Does SwingBuilder provide some kind of an app framework to manage all the pieces involved in writing any decent Swing app. Does SwingBuilder at least give the ability to import a matisse created screen layout into the app for cases when manual layout is just too much? If SwingBuilder can answer these question in a satisfactory manner then I'd be very interested. And I didn't even get into effects, and animations.
  • Danno Ferrin: ... Speling Errors Thursday, March 29, 2007
    [Trackback] Geertjans been doing great things with Groovy and NetBeans. Given how easy this all is in Groovy, I wonder whether the Groovy community even needs a Matisse-like GUI Builder. It all seems pretty intuitive, and with syntax coloring and code...
  • Geertjan Thursday, March 29, 2007
    How funny! I'm on the other end of the 'Matisse is cool' debate, apparently. Haha. That's never happened before. Believe me, I'm going to do everything I can to get Matisse to work for Groovy, very high up on my list of things to do. Not sure if it will be possible, though. And on the other points, Mike A, I'm hoping some SwingBuilder guru is going to stop by and answer those questions, better than I'd be able to.
  • Guillaume Laforge Thursday, March 29, 2007
    Mike A, you should have a look at the "Groovy in Action" book, it explains all those things: you can of course insert any swing widget, and complex nested components, you can add event handlers, etc.
  • BobN Thursday, March 29, 2007
    Now, replace SwingBuilder() with NetBeansComponentModulePlatformBuilder() and now we got something!!!
  • MM Thursday, March 29, 2007
    NetBeansComponentModulePlatformBuilder() !!!!
    HOW to use it ? Do is it really exist?
    If so I think Netbeans is a better RCP platform compare with Eclipse.
  • Steve Friday, March 30, 2007
    I'm pretty new to groovy but I would love to see a groovy ide written in groovy for groovy. That way the ide can be customized on the fly ala smalltalk.
  • Gregg Wonderly Wednesday, April 4, 2007
    The most interesting thing about examples like these, is the question of 'conciseness'. Is it the fact that the representation is concise which makes it exciting, or what.

    Ten years ago, I had been writing TCL/TK GUIs. I liked the command interface for layout in TK. So for Java, I created a wrapper around GridbagLayout which is visible in the http://packer.dev.java.net project.

    Using Packer, I would design the above (without the menu, which we know how to do) as.

    Packer pk = new Packer( frame.getContentPane() );
    int y = -1;
    pk.pack( new JLabel( "Label 1") ).gridx(0).gridy(++y);
    pk.pack( new JSlider() ).gridx(1).gridy(y).fillx();
    pk.pack( new JComboBox() ).gridx(2).gridy(y).fillx(0);

    With the complete layout of the component expressible on a single line of code, do you feel like you still need a GUI builder?

    I use netbeans everyday, but due to the overhead of navigating all the matisse property sheets etc., I still code GUIs by hand. I can get it done much faster, I feel anyway.

  • Fleming Wednesday, April 4, 2007
    I'm not very familiar with Groovy, but it looks like a script syntax to define UI layout. Perhaps simpler than Swing, I guess.
    But for more complex forms, something like Matisse would make life a whole lot easier, and faster to make modification. Especially for those fine pixel movements.
  • 礼仪公司 Monday, April 30, 2007
  • Glen Pepicelli Thursday, June 28, 2007
    >>This is all well and great, but this is really a very trivial
    >>example to draw any serious conclusion from. I don't
    >>know Groovy, or SwingBuilder for that matter, but for
    >>instance how do you wire actions, and events? How can
    >>you use external resources for menus, actions, icons,
    >>and such
    You can do anything you can do in Java with Groovy and SwingBuilder. Groovy is a complete language and all SwingBuilder really does is call a bunch of groovy methods for you-- it's not a markup language. So if SwingBuilder doesn't do something you just use swing directly. There are examples in the two Groovy Books.
    And for an action it's:
    builder.frame( title: 'hi there')
    {
    button(
    text: 'click me',
    actionPerformed:
    {
    print 'you triggered an action';
    someothermethodcall()
    }
    }
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.