Java Reference
In-Depth Information
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() { new ProgressTrackingFrame(); }
});
}
Try running the program again and observe what happens. Finally, it is good to know that you can
also call get() and get(long timeout, TimeUnit unit) on a SwingWorker object to block until
the background task has completed and you get the final result. In most cases, this blocking behav-
ior is not what you want, and it is a better idea to have the done method of the worker call one of
your objects to notify about the completion.
You've now seen all the important things you need to know about GUIs in Java. With a complex class
hierarchy, a huge list of components and containers, layout managers that all have different behav-
ior and a plethora of event listeners, building GUIs in Java can appear to be a daunting task at first.
Don't be afraid to get started and build some simple interfaces. Check the online documentation and
Eclipse's helpful autosuggest feature to get to know Swing's components and utilize them whenever
you can. Exercise is key here, and this chapter has only outlined the basics. The final chapter in this
book contains many case study examples that will have you building more realistic, complex graphical
user interfaces, utilizing many of the available Swing components. Don't hesitate to have a look.
closing topics
This chapter closes with some additional topics related to the concept of GUIs.
Best practices: keeping looks and logic separated
A first topic you'll read about relates to best practices when writing graphical user interfaces.
Generally speaking, even after gaining knowledge of Object‐Oriented Programming, beginners will
find it relatively hard to keep domain logic separated from user‐interface concerns. When you find
yourself making monstrous user interfaces where buttons and text fields keep track of state, you'll
know it is time for a change.
Ideally, you should be able to perform all your application logic using pure Java objects and without
using any graphical user interface at all. Therefore, it is always a good idea to construct your core
domain concepts first. Which concepts exist? What data needs to be stored? Which actions can be
undertaken? Afterward, you can start building the GUI around all of this. As a challenge, consider
creating a “console” version for your applications as an alternative to the graphical one. How hard
would it be? Where are the main root issues? Here lies the opportunity for solid refactoring.
The next chapter talks more about patterns, and you will get acquainted with a very helpful pattern
when building GUI‐driven applications: the model‐view‐controller pattern.
Although separating your logic from UI is a must, it is also a good idea to keep your UI as architecturally
sound as possible. Many of the examples you've seen in this chapter already reflect a natural progress in
UI architecture, starting with sticking everything in a main method (bad) to creating custom UI classes
 
Search WWH ::




Custom Search