The Griffon Trove: checking EDT violations
Executing code in the proper thread is a must for every Swing application, problem is that it's so easy to break the rules. Griffon is aware of this and tries its best to help you deal with the complexity of running code in and out of the EDT.
Since version 1.0.0 of the Swing plugin you can instruct the running application to warn you when an EDT violation occurs. Take the following code for example
griffon-app/models/sample/SampleModel.groovy
griffon-app/views/sample/SampleView.groovy
griffon-app/controllers/sample/SampleController.groovy
Running this application with the EDT violation checker enabled throws some interesting results when the button is clicked.The controller performs 3 tasks
- Check if the current thread is the EDT or not. By default all controller actions are executed outside the EDT so this check should succeed.
- Change the value model.text which should trigger a change in the UI. Groovy bindings are aware that the change will be propagated to an UI component, sending the value in the correct thread.
- Change the button's text directly. This change breaks the Swing rule which is why we get a logged exception.
Keep on Groovying!
About Andres Almiray
Andres is a Java/Groovy developer and Java Champion, with more than 11 years of experience in software design and development. He has been involved in web and desktop application developments since the early days of Java. He has also been teacher of computer science courses in the most prestigious education institute in Mexico. His current interests include Groovy and Swing. He is a true believer of open source and has participated in popular projects like Groovy, Griffon, JMatter and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member and current project lead of the Griffon framework. He blogs periodically at http://jroller.com/aalmiray. You can find him on twitter too as @aalmiray. He likes to spend time with his beloved wife, Ixchel, when not hacking around.
More About Andres »SpringOne 2GX
October 15 - 18, 2012
Washington DC
Current Topics on the NFJS Tour
- Core Java, JEE
- Dynamic Languages: Groovy, JRuby, Scala, Clojure
- RESTful Web Apps
- Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
- Agility
- Test Driven Design
- Security
- Ajax, Flex, RIA
NFJS, the Magazine
2012-12-01 00:00:00.0 Issue Now AvailableFunctional Thinking Part 1
by Neal FordBetter Together Than Alone: Pull Requests
by Tim BerglundXSLT in Groovy: Quick and Dirty
by Nick Watts