JavaFX: Using Patterns & Clean Code
We are seeing quite a number of exciting JavaFX demos around, demonstrating the pretty features of the language and the capability of easily integrating cool graphics. But, as a software designer, I can't prevent myself from seeing that in most examples we see bloated code - no good separation of concerns and poor applications of the MVC pattern. This is reasonable, as JavaFX was a new language and people needed first to be taught about the syntax and features; but now that - I presume - many of us have been introduced with the new language, it's high time we started worrying about good practices, as well as writing self-commenting code. Let's remember that good practices and good design are always more important than the language!
Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.So, let's introduce a very simple project - a Contact List whose specifications are:
- We have a list of "contacts", where each item is name, last name, email, phone, etc...
- The UI shows a list of contacts, that can be filtered by a text field; if you enter a value in it, only the contacts whose name starts with the entered text are shown. Selections are applied as you type.
- Selecting an item in the list of contacts, the details are shown in a form, where you can edit them.
- At any selection change, the form is animated (a rotation and a blur effect).

You can get the code from:
svn co -r 37 https://kenai.com/svn/javafxstuff~svn/trunk/ContactList/src/ContactList
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:






Comments
Juha Vainikka replied on Tue, 2009/05/12 - 3:30am
Great article Fabrizio!
I also think it's very important (especially for newbies) to see technical demos that aren't essentially "hacks".
Looking forward to read your next piece.
/juha
Camilo Arango replied on Tue, 2009/05/12 - 10:23am
Fabrizio Giudici replied on Tue, 2009/05/12 - 1:52pm
It's one of the things I'll try in future. For now, the only thing I've done is to run javap on a compiled JavaFX value object, and - as expected - I saw it's very different from a regular JavaBean. Basically every attribute its an independent object, I presume with its own get()/set() and binding support. But there are also lots of other stuff that I don't understand at a glance.
Fabrizio Giudici replied on Wed, 2009/05/13 - 10:18am
John Smith replied on Wed, 2009/05/13 - 12:29pm
Fabrizio Giudici replied on Wed, 2009/05/13 - 1:51pm
The fact that you do things in JavaFX "better" than Java is subjective, at this time, and related to a number of things. For me, I don't see compelling reasons for turning my Java desktop stuff into JavaFX _now_. If I had some compelling graphics requirements, at the point to throw a graphic designer into the team, JavaFX would make the difference. I expect the two different perspectives to get closer and closer in the next years.
Objectively, JavaFX is more coincise and binding is the key trick. Try to write the same example I'm talking of in Java. Of course, the same functional binding could be introduced in Java (thinking of it...) thus even Java programs could be much shorter. But with Java binding can't be as natural as is with JavaFX.
Raghu Semburakk... replied on Thu, 2009/05/14 - 4:01pm
Fabrizio Giudici replied on Fri, 2009/05/15 - 2:14am
Krzysztof Kula replied on Mon, 2009/06/01 - 12:54pm
BTW, there is JavaFX 1.2 with Linux support.
Regards,
Krzysztof Kula
Fabrizio Giudici replied on Tue, 2009/06/02 - 6:16am
Krzysztof, no, I didn't see problems with that handful of names. BTW, I have another application where a similar trick is used and it works with about 1.000 items, with only some slightly noticeable delay in some cases. Of course, with such a big number of items you probably have to do some smarter filtering rather than using subsequences.
Fabrizio Giudici replied on Thu, 2009/06/04 - 10:53am
Krzysztof Kula replied on Fri, 2009/06/05 - 4:56am
Then I must start serching for a reason in my computer platform. Regards, Krzysztof Kula
Fabrizio Giudici replied on Sun, 2009/06/07 - 11:04am
I've posted here a screencast of the application running with about 1.000 elements. The screencast is neither cut nor accelerated, you can see the real speed on a MacBook Pro.
For what concerns my original demo, the only problem I see on JavaFX 1.2 is some parts of the layout screwed up.
Pablo Oliveira replied on Sat, 2011/06/04 - 5:24pm
Matt Coleman replied on Tue, 2012/07/31 - 1:45am
in response to:
Camilo Arango
web designer buffalo