Glazedlists + Groovy: not your regular icing - No Fluff Just Stuff

Glazedlists + Groovy: not your regular icing

Posted by: Andres Almiray on May 13, 2008

I been wanting to write about GlazedLists + Groovy since 2GX, as James and I made a demo of a Grails/Swing powered RSS/Atom aggregator that, among other things, took advantage of GlazedLists to create JTable, scratch that, a sorted, observable JTable with minimal code. Here is a screenshot of what we accomplished so far



This is the result of querying the Cosmos status of a feed's url, the code itself is hardly 150 lines (half of it being the XML parsing to account for missing/null data). Because Groovy has blurred the line between beans and Maps getting a sorted, observable JTable model is quite easy. The first thing we require is an EventList, GlazedList's basic building block, basically it fires events whenever the list changes (even if the element's properties change too). With GlazedLists you can update your data as you'd like and all the adapters and connectors will update the UI at the appropriate time, with that in mind we create the EventList as follows

That's right, we are taking advantage of Groovy's powerful as keyword to coerce a closure into a dynamic proxy of a Comparator, in this case will sort elements based on their name property, which may anything that implements the Comparable interface (such as Strings). The second step is to wire a JTable with the list, say what? exactly join a List with a JTable. GlazedLists has several ways of doing so, I'm going to show the one using a helper class: EventTableModel and its companion: TableFormat. This interface is responsible for expanding each element of the list into the proper row values it represents. In the case of the Technorati stats gatherer, each list element is actually a Map with 4 properties: name, url, blogs and links; each column will use the capitalized property name as its header, thus making our job a lot simpler

Again we reuse the coercion trick but this time with a Map, it is so simple and addictive it should be illegal (or criminal as Shemnon recently said). Remember that this trick has a drawback: no inheritance (unless you use method pointers which tend to muck the trick). This is one of the reasons I rarely miss inner classes when using Groovy. Back to the code, the next step is wiring up the table and enabling the sorting mechanism, if you were expecting complicated code here please go to your regular Java Swing cookbook examples, no such thing here!
Yup, it only takes one line! Now we are able to update the data as we see fit, the connectors/adapters from GlazedLists and Swing will take care of updating the UI, our Technorati stats gatherer does it in the following manner
So there you have it. If your daily work includes mangling data back and forth with Lists and TableModels, please consider using GlazedLists (with Groovy it is even better) and stop the Swing pain.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »