JavaFX: Using Patterns & Clean Code
The Views
Now, a UI component. The way we design it largely depends on the process, as a graphic designer could be involved. In any case, I think that the whole UI should not be implemented in a single, bloated class; rather relevant pieces should be split apart. For instance, a CustomNode can model the "form" that renders the contact details (in the code below I've omitted all the attributes related to rendering):
package it.tidalwave.javafxstuff.contactlist.view;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.Node;
import javafx.ext.swing.SwingLabel;
import javafx.ext.swing.SwingTextField;
import it.tidalwave.javafxstuff.contactlist.model.Contact;
public class ContactView extends CustomNode
{
public var contact : Contact;
public override function create() : Node
{
return Group
{
content:
[
VBox
{
content:
[
SwingLabel
{
text: bind "{contact.firstName} {contact.lastName}"
}
HBox
{
content:
[
SwingLabel { text: "First name: " }
SwingTextField { text: bind contact.firstName }
]
}
HBox
{
content:
[
SwingLabel { text: "Last name: " }
SwingTextField { text: bind contact.lastName }
]
}
HBox
{
content:
[
SwingLabel { text: "Email: " }
SwingTextField { text: bind contact.email }
]
}
HBox
{
content:
[
SwingLabel { text: "Phone: " }
SwingTextField { text: bind contact.phone }
]
}
]
}
]
};
}
}
As you can see, we have only layout and data binding here, the only things a view should do.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






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