 |
Finally... Client Properties You Can Use
Posted by javaben on April 13, 2006 at 09:42 AM | Comments (11)
One of the Java Swing GUI toolkit's strong advantages is its flexible, easy-to-customize architecture. Swing isn't easy for beginners, but once you grok it, there are a lot of great ways to customize the behavior, state, and appearance of a widget. Indeed, if maximum flexibility is your goal, you'd be hard-pressed to find a better toolkit on any platform.
I used to hand-code all of my Swing code, including UI layouts. Thankfully, over the past year or two, several high-quality graphical UI layout tools have emerged, the first of which (that I noticed) was JFormDesigner -- which I still use. But there's one feature I really miss from the current generation: client property support.
I find myself constantly using client properties to extend the functionality of Swing components. For example, I'm a big fan of externalized formatting for Swing components, like what Guy Romain is doing with the Fuse project. While Fuse is annotation based, I'm a simpler guy -- I want to be able to define properties of Swing components in an external file (very much like a CSS file) and then apply those styles to Swing components by id (which I map to the JComponent's name property) and by "class". So, I can do something like:
JTextField.productCode {
columns = 10;
fontSize = 10pt;
font-family = Helvetica,Arial,Dialog;
border: 1dlu solid black;
}
and map that to all JTextField's with the "productCode" class. Ah, so how do I assign the productCode class to a component? Subclassing is of course a bad idea. I maintain that you should never, ever subclass Swing components to add general functionality to them (if you ever hope to leverage a single third-party component).
The answer, of course, is a client property. I can define a client property key "org.galbraiths.clarity.styleClass" and set the value to "productCode", and I've now got this great system for allowing all JComponent's to specify a class for styling. (I actually apply the styles via a decoration mechanism that applies to all component hierarchies before they are displayed, but that's another story.)
Alas, the current versions of JFormDesigner, NetBeans, and IDEA don't provide any support for setting client properties. Ugh. So I have to write some kind of custom code all over the place to do this, which I'm loathe to do. I suppose I could, but, frankly, it makes my WinForms developer friends laugh at me, and I hate that.
Fortunately, that's about to change. The next version of JFormDesigner introduces support for client properties. You go into the Preferences menu and define your standard client property keys, like so:

and then the property pallette lets you set those values:

Nice! I'm one step closer to my dream development environment for my Swing framework. The next version of IDEA's GUI builder will also support client properties; hopefully, NetBeans will too.
By the way, I'll be talking more about this, and will be sharing some of my code under a license anyone can use, at this year's JavaOne in my Swing session. If I've said something you particularly disagree with, make a rude comment to this blog, by all means, but even better -- come and heckle! Chet can't be the only one with rabble-rousers in the audience.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
I haven't used JFormDesigner a lot but from what I have seen so far it's really a great tool and I'm glad to see its author is bringing innovations to it. Anyway, I'm looking forward to attending your session. And I promise I'll heckle (I just need to find a reason now.)
Posted by: gfx on April 13, 2006 at 11:01 AM
-
Nice! I've been thinking of some really cool stuff for Dolphin that will use client properties, so I'm glad to see there is some IDE support. We need to have a big list of the commonly available client properties somewhere.
Posted by: joshy on April 13, 2006 at 03:12 PM
-
gfx: I'll be sure to leave some time for Q&A this year ;-)
joshy: Sounds like a great idea. Would just be a 30 minute project, too. Maybe someone with some free time reading the comments can do it? Post a blog entry? Please? ;-)
Posted by: javaben on April 13, 2006 at 03:27 PM
-
If I use JFormDesigner, how much am I locked into it? I looked at it, but was afraid of being tied to it.
Posted by: coxcu on April 14, 2006 at 05:55 AM
-
Very interesting...Is this external CSS mechanism you describe something that is available with Swing or would it be custom code that I would need to write?
Posted by: robjkc on April 14, 2006 at 07:43 AM
-
>> (I actually apply the styles via a decoration mechanism that applies to all component hierarchies before they are displayed, but that's another story.)
Actually I'm more interested in how you do this. I also use client properties and set them in a way that's visitor patternish. It works OK but I could think of a few ways it could be done. I've never seen anyone else's code that implemented custom client properties. I came to it on my own when I hit a multi-inheritance brick wall.
Ray
Posted by: raytucson on April 14, 2006 at 08:22 AM
-
coxcu: Unfortunately, there's no standard for a GUI builder file format that allows you to transfer your GUI designed screen from builder to builder. That would be the ideal solution to prevent lock-in, but would also mean all kinds of little problems like builder A supports X, Y, and Z but builder B does not, etc.
Having said that, I'm not sure I understand the problem. I have an abstraction layer in my application that loads the product of any GUI builder and presents it to my application as a Swing component hierarchy. Other than four lines of code in one place that loads the screen definition, there is no other JFormDesigner-specific code in my system.
As far as the artifacts that JFormDesigner generates, its either Java source code or a JavaBeans XML serialization file, both of which are fairly standard.
So, lock-in isn't a real issue for me using JFormDesigner or any other GUI builder -- at the end of the day, they all have to generate a Swing component hierarchy.
robjkc: Yeah, its definitely not included in Swing at the moment. At my JavaOne session this year, I'll talk more about the mechanism I use and give attendees source code they can use to add CSS-like decoration to their own apps.
raytucson: My framework is a container-based form framework. So, each screen you display is a form, and a container manages the initialization of that form. As part of the initialization, the container will decorate the components in the form. This decoration covers a lot of extensions I add to general Swing mechanisms. For example, I wrap all of the TableCellRenderers in every JTable to provide support for changing column alignment in a generic way (among many other features). In this case, I apply styles to components.
One problem I haven't fixed with this approach is what you do for dynamically created components. At the moment, you have to manually ask the decorator to decorate such manually created components. But, that's not a terribly common case for the particular class of application I designed the framework to handle, and there may be some ways to automate the discovery and decorate of dynamically created components in this case -- I haven't looked into it.
To put it all in more specific terms, here's some pseudo code that demonstrates how it works:
JPanel panel = ...
FormContainer container = ClarityManager.installFormContainer(panel);
container.displayForm(new MyForm());
The code above would cause a FormContainer to display (and initialize, and as part of the initialization, decorate) the MyForm instance. MyForm looks a bit like:
public class MyForm extends Form {
public void createAndLayoutComponents(Container container) {
useRuntimeForm("MyForm");
}
public void attachListenersToComponents() {
bindAction("componentName", someAction);
}
}
You can do whatever you like in createAndLayoutComponents to initialize the Form's component hierarchy. The method useRuntimeForm(String) will look for a GUI builder artifact named "MyForm" in a special place in the application and load it. "MyForm" can either be a .jfd file (JFormDesigner's format, which is just the JavaBeans XML serialization output format) or some other GUI builder -- the abstraction layer takes care of all that.
So, those are a few more details on how things work in my world.
Posted by: javaben on April 14, 2006 at 09:11 AM
-
Very nice. Especially having the ability to see the effect of the style in the form editor.
I wonder how you deal with Look and Feel differences? I guess you must be using a different stylesheet for each L&F. The fontSize and font-family values in your JTextField.productCode selector obviously aren't right for Mac OS X.
Posted by: wrandelshofer on April 15, 2006 at 01:12 AM
-
wrandelshofer: I typically use a cross-platform look-and-feel precisely so I don't have to worry about managing different styles for different platforms, etc.
But, if you did want to do such a thing, you could do some fancy condition stuff, like:
AquaLookAndFeel.small {
font-size: 10pt;
font-family: Helvetica;
}
WindowsLookAndFeel.small {
font-size: 9pt;
font-family: Arial;
}
.productCode {
font-size: small;
font-family: small;
border: 1px solid black;
columns: 10;
}
Posted by: javaben on April 15, 2006 at 01:40 AM
-
Thanks for very interesting article. btw. I really enjoyed reading all of your posts. It’s interesting to read ideas, and observations from someone else’s point of view… makes you think more. So please keep up the great work. Greetings. Firefox下載,Thai Boxing,鋼琴搬運,搬屋公司
Posted by: winbill on December 19, 2007 at 08:26 PM
-
lotro gold
lord of the rings gold
lord of the rings online gold
lotro gold
lord of the rings gold
lord of the rings online gold
Warhammer gold
Warhammer online gold
Warhammer money
War gold
War money
Tabula Rasa Credit
lotro gold
lord of the rings gold
lord of the rings online gold
lord of the rings online gold
lord of the rings gold
Tabula Rasa Credit
World of Warcraft gold
PotBS Doubloons
Pirates of the Burning Sea Doubloons
PotBS Gold
Pirates of the Burning Sea Gold
lotro gold
lord of the rings gold
lord of the rings online gold
Tabula Rasa Credit
Warhammer gold
Warhammer online gold
PotBS Doubloons
PotBS gold
Age of Conan gold
Age of Conan gold
Pirates of the Burning Sea gold
Pirates of the Burning Sea Doubloons
Warhammer gold
Warhammer online gold
Age of Conan gold
PotBS Doubloons
PotBS gold
Pirates of the Burning Sea gold
Pirates of the Burning Sea Doubloons
Posted by: huang_lar on March 16, 2008 at 12:57 PM
|