JRuby can save Swing

Julian Doherty

Swing is hard. Needlessly so. With the advent of scripting support on the JVM, why not wrap a light and elegant Ruby layer around Swing? Why keep coding Swing at it’s lowest API level using a heavy-weight language like Java?

I’ve always maintained Swing is a nice framework. It’s just that it’s too low level and Java is a lousy language for programming it in. It’s already possible to code Swing in Ruby by using JRuby. You get some of the Ruby niceness (such as easier method calls, some dynamic typing, and blocks), but you still end up fighting against listeners, layout managers, look-and-feels, event-threads etc before long. Suns trademark API over-engineering casts a long shadow, even into Ruby land.

The Swing API itself is very flexible and powerful, but at a low level. It’s powerful in the same way that a coil of wire and a bunch of tools is a flexible and powerful way to configure the electricals in your house. It’s just that sometimes you only want to plug a few things in and flick a switch to watch a DVD.

So what specifically are the problems?

Swing’s issues

Joshua Marinacci’s blog post Swing has failed. What can we do? from 2003 covers this issue nicely. Joshua points out these flaws which I’d agree with 100%. Nothing much has changed in the years since, except maybe that Swing looks somewhat better now and resembles native apps much more closely.

  • "Swing apps are slow to build."
  • "Swing layout managers suck."
  • "Swing apps are hard to maintain."
  • "Swing is too powerful."
  • "No native features."
  • "Swing apps have a bad history."

What needs to be done?

Joshua’s ideas on what needs to be done to fix Swing are interesting.

(there are more points he makes, but I’m just including the relevant ones)

  • "A structured way of building Swing apps. Something like Struts where each concern (aspect?) has it’s own special place to be stored and manipulated. How do you organize your layout, workflow, validation, intz’ed text, and hooks to the BL? Should you subclass to make frames or use factory code? We need a set of best practices and then a framework to implement them."
  • "A standard cross-tool representation of a GUI that is not Java code. Something that can be moved in and out of different IDEs and build tools. Probably an XML representation or maybe serialized Swing objects."
  • "An intermediate API. Maybe we need a simpler API on top of Swing? A new set of wrapper components which hide a lot of the complexity. Hell, half of the work could be done by just conditionally hiding 3/4 of the API from the Javadocs."

I particularly like the idea of a structured way of building Swing apps. At the moment it’s a free-for-all. Some developers have come up with nice individual patterns for maintaining order, others implement spaghetti, most start out well, but degrade into entropy. This even applies individual screens in some apps! - I’ve seen 6000 line long, single-class monstrosities for a single window! One of Rails main strengths is it’s “Convention over Configuration” approach for separating out and structuring an application. There is no reason why this can’t apply to a Rich Client.

My Ideal Swing/Ruby framework features

So… what would this look like?

  • Structured application layout. Works for Rails. Nuff said.
  • Proper decoupling of model from view. Probably using Martin Fowler’s Presentation Model pattern, or similar.
  • Automatic binding of model to view without glue code. (this makes the last point much easier to implement.) Ruby is perfect for this. You should be able to write something like name_field.bind_to song, :title and be done with it. Ruby should do it’s thing and set up listeners automatically - Even if the model class wasn’t designed with listeners when it was coded.
  • View logic code separated from layout markup. View components and interaction (enable this button with this tooltip etc) should be coded in Ruby. Layout should be stored in a YAML file or similar (please no more XML…). Layout via coding is far too hard for most people to use effectively. Minimize the pain and let the framework take care of the grunt work. We definitely DO NOT want anymore GridBag boilerplate code…
  • Application lifecycle support. Related to having a structured application layout. Provide session support, long lived models etc, and handle startup/shutdown.
  • Minimize the amount of code. You already get this for free by using Ruby, but removing the fluff from Swing is a must. Too much code to achieve too little. It should be possible to scaffold something basic like Rails does, then tweak it if it needs to do more. DRY is part of this, but that just seems like basic common sense for any kind of development in any language.
  • Minimize complexity. Related to minimizing code. Making it easy to use a proper pattern like Presentation Model and removing a lot of the spaghetti listener code by using bindings will help a lot.
  • Testable. It’s really hard to write automated tests for GUIs. Libraries like JFC Unit make it easier, but it’s still pretty heavy going. I don’t know the answer to this one. GUIs always need at least some manual intervention to check the visual layout (a computer can’t tell if it doesn’t “flow” right). Maybe the framework could help by making it easy to substitue mock objects for testing the presentation model code.
  • Make the Right way of doing things the path of least resistance.. This one is important, and I think it is one of Swing’s big weaknesses. This is related to having a good structure, but it goes from there all the way down to how easy the APIs are to use. Rails makes it easy to create good designs and stick to them. Swing makes it hard - as evidenced by the plague of badly thought out Autonomous Views espoused by Sun’s Swing tutorials

Even a few of these points would make Swing much more productive and fun. I’ve made a few posts recently that are leading up to this. Over the next few posts I’ll start to experiment with a few more of the pieces to see how it all hangs together. Who knows, maybe I’ll actually get something usable at the end :)


12 Responses to “JRuby can save Swing”

  • Steve Says:

    I agree with the sentiment, but not to craft yet another solution.

    Why don’t we just call this Eclipse or NetBeans RCP?

    Really these things have been done, what we lack is people understanding and using them.

    There is a JSR on the table for a Swing framework too - JSR-296, this will help quite a bit.

  • Anonymous Says:

    Blech, JRuby.

    Groovy already has a SwingBuilder, which I suppose you could duplicate in Ruby… but most of these are done arleady.

  • Mago_Ged Says:

    Quote mr.Anonymous. Groovy already has most of this: try SwingBuilder.
    I take chance to say that java is the most fortunate thing could ever happen to ruby…

  • fsilber Says:

    Is Swing hard? Many tools may make it easier to craft a “one-of” application form, but what tool makes it easier for the average programmer to create and re-use one’s own application-oriented higher-level GUI components?

  • daniel Says:

    Gosh… I find Swing the easiest and nicest part of Java. Yeah it’s almost always too verbose but it just makes a lot of sense to me. Especially considering how flexible it is. I find I can just code off the reference documentation. It’s the other commonly used stuff in Java like the various ORMs that just drive me crazy. However, it’s true that a nice JRuby swing could be simply… incredible.

  • Julian Doherty Says:

    steve - I have to admit I haven’t used the Eclipse or Netbeans RCP projects. It seems to be that they both suffer from the Java “put it in a container, make it endlesly configurable with XML” design style. That works a lot of the time, but there is also value to Rails “convention over configuration” approach.

    The other reason is that if you’re using Eclipse or Netbeans RCP, you’re working in Java. Java is heavy and verbose code. Sometimes you need the type checking and performance, but a lot of the time (for a simple GUI), a dynamic and more relaxed language like Ruby can be more productive.

    anon - (I’m not that familiar with Groovy either.). True, Groovy could work. From what I can tell though you just get a nicer way of writing the same old Swing.

    fsilber - A tool for creating usable GUI components? I’d call that Swing ;). Seriously though, I’d envisage that Swing is the low level that advanced developers would create the building blocks in. A framework like I’m talking about here would just make it easier to use them.

    daniel - Swing is easy once you’re up to speed, but it’s got a steep learning curve, and even when you know what you’re doing, things still take longer than they should. I figure you should be able to do the same things easier, and in less time with Ruby.

  • paawak Says:

    Julian, I do empathise with you. You remind of the days when I was learning to code in Swings. Your arguments are valid and I do agree its not for an average programmer to create a Awing App out of the box. And that goes a long way to explain why Swings has been a total failure. But I would also like to add Swings is a very powerful framework and not that cumbersome once you get used to it. But if recent trends are to be believed, there is a definite upwards swing for Swings.

  • Anonymous Says:

    clearly Java is perfect language for Swing, because Swing was designed for Java. It is powerful, but it’s easy to build and wrap an intermediate framework around Swing. This can be done in a week or two, and i have done it myself quite a few times, for each project/client. We are just now starting to see new Swing frameworks (eg. jmatter, genesis, jsr296) and this trend will continue, in addition to the RCPs of Netbeans and Eclipse.

  • Anonymous Says:

    Swing’s learning curve isn’t anymore steeper than learning html, ajax framework of the week, css, javascript + dom and a little bit of flash.

    People poo poo about Swing, but really I don’t see web app developers poo pooing about all the toolkits necessary to build web ui’s. I agree that higher level stuff is great; but to be an ‘expert’, I think that the learning curve is a given to master all the internals + guts of any technology.

  • Anonymous Says:

    What did you think of Daniel Spiewak’s blog on the matter? http://blogs.dzone.com/daniel/
    2007/02/21/does-swing-need-saving

  • Julian Doherty Says:

    anon - I liked Daniel Spiewak’s reply, and I’ve posted a response to that and some other replies at JRuby Can Save Swing - Follow Up

  • Sergio Says:

    I think you are right ! But the problem is not Swing or Java itself but the lack of an application framework. I agree, Swing is just a low level API for graphical widget. Without a framework that defines the Application concept, without a true well integrated databinding mechanism, action framework…is really hard to build a real word GUI application (especially data-centric ones).
    But i think there is no need to use something different from Java. If you take a look on how a very old product (but for building desktop applications IMHO allready the best one) like Borland Delphi (and especially the VCL framework) works you can see that all Delphi does can easilly be reproduced with Java.
    The Delphi’s strength is the VCL framework, the Ruby strength is the Rails framework, Java is a good language and a very powerfull architecture, just a good GUI application framework is needed.

Leave a Reply