Over a million developers have joined DZone.
Platinum Partner

Java Swing Components: Bringing Your Swing UI Back To Life

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Last week, I discovered a set of Java Swing Components based on a tweet from Kirill Grouchnikov. It's a long time since I saw something new in the Swing space, so  I took a look at the components and contacted the lead developer for the project, Rhiannon Liebowitz, to find out more about his component set, and his plans for it's future. 

DZone: What is your experience with Swing?

Rhiannon: I first encountered Swing while I was working for a large financial organization. The organization was planning to build a banking application to be deployed into more than a dozen countries in Africa. This was quite a few years ago, and bandwidth would be one of the largest constraints we would encounter. As a result a thick Java client was decided upon (as the data transferred by a thick client is much more efficient than an HTML web based system) and I was lucky enough to be one of the first developers assigned to the project.

During my years on the project I was responsible for building a large number of custom Swing components as well as developing various frameworks on which the application relies. It was during this time that I came to love Java and Swing. My favorite aspect of Swing is its flexibility; it gives you the power to build almost any component.

DZone: What motivated you to create these components?

Rhiannon: There were essentially three factors that motivated me to create the initial set of components. First, I wanted to show that Swing components can be visually impressive. Second, I wanted to build components that would be useful to all Swing developers. Third, I wanted to fill a gap in the functionality that exists in the standard Swing libraries.

Working as a Swing developer I noticed that one of the most common frustrations encountered by developers (and business analysts alike) is the inability of the standard JLabel to wrap text, handle line breaks or mix fonts and colors without the inclusion of HTML. Personally I am against the inclusion of HTML in a label’s text as I feel it violates the MVC pattern, pollutes the label’s text and results in string parsing. To make matters worse, trying to perform rule based formatting by generating HTML tags to be included in a label’s text opens a particularly nasty can of worms.

The Wrapping Label was created to address all of these issues, and it does so by making use of a formatting rule engine. The engine processes a list of rules defining how a string should be rendered and applies the rules to a string during the painting process. This keeps the label’s text totally separate from its formatting.

The Accordion was also intended to fill a gap in the swing library. Most other libraries (particularly web based libraries) come standard with an Accordion type component. However, unlike the Wrapping Label, the Accordion gave us the opportunity to really make an impressive looking component and demonstrate how great swing can look. The Accordion, although extremely similar to a JTabbedPane, can be more practical in certain circumstances. An Accordion excels at making the most use of limited horizontal space. When a JTabbedPane is constrained horizontally, the tabs get placed one on top of another and can become hard to read. An Accordion does not suffer from this problem.

One of our goals is to develop a set of components to be used on dashboards and widget libraries. Not only is the Analog Clock the first component of this set, but it is also a component that does a great job of adding a touch of polish to desktop applications. The clock paints itself using a layered approach, thereby making it extremely easy to tweak and customize to the developers needs.

Can you give us some example of how to use the components?

Rhiannon: A great example illustrating the power of the Wrapping Label and its rule based formatting engine, would be to render an approximation of the JAVALOBBY logo.  

To achieve this I will create a new Wrapping Label and then apply the following formatting rules to the label:
1.    The word ‘JAVALOBBY’ should appear blue using size 38, Cambria font.
2.    The first letter should be size 43.
3.    The first occurrence of the word ‘heart’ should appear red and underlined.
4.    The characters ‘(TM)’ should always appear as a superscript.

One of the greatest advantages of the formatting engine is the ability for developers to create and add their own rules in order to cater for even the most unique scenario.

The code below demonstrates how to create the label and apply the rules.

The output above has been achieved without the use of any HTML, resulting in the label’s text remaining pure

DZone:  What are your plans for future development?

Rhiannon: We have a number of new components in various stages of development. Our next scheduled component for release is a date picker (calendar) control.

Future components include:
•    Hyperlinks
•    Auto complete text boxes
•    Digital clocks
•    5 Star rating components
•    Additional sets of borders and separators
•    And many many more.

One of the greatest challenges we are facing at the moment is to prioritize the development of our components to best suit the needs of the Java community. We would love to hear from swing developers out there who have a need for any new type of components.

DZone: What do you think of JavaFX?

Rhiannon: I must admit that JavaFX does look extremely promising and intrigues me greatly; however at present I have not allocated it enough time for the attention it deserves. Having spent most of my time developing banking software, I have learnt to be relatively risk adverse when it comes to new and bleeding edge technology, and as a result I like to wait a few years for a platform to stabilize and entrench itself in the community before I devote substantial time to it.

Do you think there is a decline in Java desktop application development, with both Swing and SWT at the moment?

Rhiannon: I definitely feel (over the years) that there has been an upswing in the popularity of web based development at the expense of desktop development. However, I still feel that to build truly rich and responsive applications, the desktop is the only way to go.

Another consideration that I feel is often overlooked is that in a surprising large number of the world’s countries, high speed bandwidth is actually a luxury, enjoyed by very few. In theory rich web based applications can be accessed from anywhere in the world, however the truth is that there are many people simply don’t have the bandwidth to use them. A thick client communicating over HTTP (or some other protocol) will always outperform a web based application, as it will only send data it needs and will not include any formatting tags. Just think how much wasted data is used to send an HTML table, with all its <tr> and <td> tags.

I truly believe that Swing is the one of the best gui toolkits available for any developer who wants to build a cross platform application, and I think many developers will be programming in Swing for many years to come.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}