Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Further with Spring RCP

07.03.2008
| 133588 views |
  • submit to reddit

Using the Form Builder

Now we use the Spring RCP Form Builder.

You will see that the Form Builder lets you

  • create the form's layout very easily

  • while, magically, removing the need to worry about the type of Swing components you want to have rendered for the fields to which you want to give your users access.

Start off by replacing the content of your existing CustomerForm with the code below:

package simple;

import javax.swing.JComponent;
import javax.swing.JTextField;

import org.springframework.richclient.form.AbstractForm;
import domain.Customer;
import org.springframework.richclient.form.builder.TableFormBuilder;

public class CustomerForm extends AbstractForm {

private JComponent firstNameField;

public CustomerForm(Customer customer) {
super(customer);
setId("customer");
}

@Override
protected JComponent createFormControl() {
TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
formBuilder.addSeparator("General");
formBuilder.row();
firstNameField = formBuilder.add("firstName")[1];
formBuilder.add("lastName");
formBuilder.row();
formBuilder.addSeparator("Address");
formBuilder.row();
formBuilder.add("address.street");
formBuilder.row();
formBuilder.add("address.city", "colSpan=1 align=left");
formBuilder.row();
formBuilder.add("address.state", "colSpan=1 align=left");
formBuilder.row();

JComponent zipField = formBuilder.add("address.zip", "colSpan=1 align=left")[1];
((JTextField) zipField).setColumns(8);
formBuilder.row();

return formBuilder.getForm();
}

public boolean requestFocusInWindow() {
return firstNameField.requestFocusInWindow();
}

}

 

Note: It seems to me that a central problem with so many strings being set in the Form Builder (and elsewhere in Spring RCP applications) is that an IDE is not going to be very helpful in providing you with hints (or code completion or intellisense, call it what you will). How is one supposed to know of the existence of properties such as "colSpan" and "align" in lines 32, 34, and 37 above? Only be reading the documentation. I haven't yet found documentation on the above properties. If I were able to use code completion in my IDE, I wouldn't need documentation at all. But strings do not encourage code completion. However, it is clear, from the above lines of code (and the screenshot below), that one can influence the width (and so on...) of fields displayed in a Spring RCP AbstractForm.

Now, when you run the application again and then invoke the Properties popup item, you should now see this:

 

However, since all our fields are strings, the true power of the Spring RCP Form Builder is not really proven yet. To drive the point home, go back to the Customer domain object. There, add a new boolean with the name "married", together with a getter and setter:

private boolean married;

public boolean getMarried() {
return married;
}

public void setMarried(boolean married) {
this.married = married;
}

 

Next, in the CustomerDataStore, add the boolean to the "makeCustomer" method:

private Customer makeCustomer(
String first, String last, boolean married, String street,
String city, String state, String zip) {

Customer customer = new Customer();
customer.setId(nextId++);
customer.setFirstName(first);
customer.setLastName(last);
customer.setLastName(last);
customer.setMarried(married);

Address address = customer.getAddress();
address.setStreet(street);
address.setCity(city);
address.setState(state);
address.setZip(zip);

return customer;

}

 

Finally, in the "loadData" method, again in the CustomerDataStore, add a "true" or a "false" to each of your customers, depending on whether you want them to be married or single:

private void loadData() {
customers.add(makeCustomer(
"Larry", "Streepy", true, "123 Some St.", "New York", "NY", "10010"));
customers.add(makeCustomer(
"Keith", "Donald", false, "456 WebFlow Rd.", "Cooltown", "NY", "10001"));
customers.add(makeCustomer(
"Steve", "Brothers", true, "10921 The Other Street", "Denver", "CO", "81234-2121"));
customers.add(makeCustomer(
"Carlos", "Mencia", false, "4321 Comedy Central", "Hollywood", "CA", "91020"));
customers.add(makeCustomer(
"Jim", "Jones", true, "1001 Another Place", "Dallas", "TX", "71212"));
customers.add(makeCustomer(
"Jenny", "Jones", false, "1001 Another Place", "Dallas", "TX", "75201"));
customers.add(makeCustomer(
"Greg", "Jones", false, "9 Some Other Place", "Chicago", "IL", "60601"));
}

 

OK. Now simply add the "married" field to the CustomerForm.createFormControl, as shown here:

@Override
protected JComponent createFormControl() {
TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
formBuilder.addSeparator("General");
formBuilder.row();
firstNameField = formBuilder.add("firstName")[1];
formBuilder.add("lastName");
formBuilder.row();
formBuilder.add("married");
formBuilder.row();
formBuilder.addSeparator("Address");
formBuilder.row();
formBuilder.add("address.street");
formBuilder.row();
formBuilder.add("address.city", "colSpan=1 align=left");
formBuilder.row();
formBuilder.add("address.state", "colSpan=1 align=left");
formBuilder.row();

JComponent zipField = formBuilder.add("address.zip", "colSpan=1 align=left")[1];
((JTextField) zipField).setColumns(8);
formBuilder.row();

return formBuilder.getForm();
}

 

Notice that above you didn't do anything different for the "married" field than you did for any of the others. One small step you need to do still at this point: add a new property in the messages.properties, for the label of the field that will be displayed on the form:

married.label=Married?

 

Now, when you run the application, you see that the "Married?" label is next to a checkbox, because the Form Builder understood that that is what needs to be created when the field's type is a boolean:

 

In summary, the Spring RCP Form Builder lets you easily create complex form layouts, without requiring you to use a GUI Builder, such as the Matisse GUI Builder. And, in addition to the TableFormBuilder that you've seen demonstrated in this section, there's also a GridBagLayoutFormBuilder and an HTMLFormBuilder, together with an AbstractFormBuilder which, I imagine, could be used to build your own custom Form Builders.

 

AttachmentSize
figure-1.png44.04 KB
figure-1-src.png35.16 KB
figure-2-src.png41.8 KB
figure-3-src.png50.43 KB
figure-4-src.png44.05 KB
figure-5-src.png44.83 KB
figure-6-src.png41.06 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Matthew Schmidt replied on Thu, 2008/07/03 - 3:25pm

Awesome article.  I love to see Java Swing apps get easier to build.  Spring RCP has been a long time coming, and I'm glad to see it finally starting to turn into something useful.

Jonny Wray replied on Thu, 2008/07/03 - 11:02pm

Great article and introduction to Spring RCP. I just wanted to comment that while version 1.0 has only recently been released the code has been useful for a lot longer. Personally, I have a couple of internal applications at work based on the framework, one of which is about three years old.

As an example of a full application, albeit quite simple, people might be interested in Bio Browser, a program to search and browse instances of a domain model from the National Cancer Institute exposed via their web services. The project page, with a web start launch is http://www.assembla.com/wiki/show/biobrowser. There is a child page on the wiki, instructions, which gives basic instructions and screenshots.

Jonny 

 

Peter Karussell replied on Fri, 2008/07/04 - 4:29pm

Thanks a lot! "Forms" was the next task for me, so you saved me a lot of hours :-)

Some more pointers that I have found for this topic (or quite similar):

I even found a full open source app (I didn't try it):

http://pegadi.underdusken.no/browser/trunk

Geoffrey De Smet replied on Sun, 2008/07/06 - 9:21am

I've added links to these articles in svn revision 2051, so they will be published on the next publish of the official spring-richclient website (which contains links to all available documentation):

http://spring-rich-c.sourceforge.net/

Geertjan Wielenga replied on Sun, 2008/07/06 - 9:57am in response to: Geoffrey De Smet

[quote=ge0ffrey]

I've added links to these articles in svn revision 2051, so they will be published on the next publish of the official spring-richclient website (which contains links to all available documentation):

http://spring-rich-c.sourceforge.net/

[/quote]

 

Great to hear! And there are more parts to this series that I am currently working on and that will be published over the coming weeks.

Lieven Doclo replied on Mon, 2008/07/07 - 2:18am

I've also written a article on how to write a custom binder:  http://www.doclo.be/lieven/articles/creatingbinderrcp.html

Geertjan Wielenga replied on Mon, 2008/07/07 - 3:11am

Thanks all, Matt, Jonny, Peter, doclolieven, for the comments and support! doclolieven, I will look at that and try it out. Jonny, can you give me some sample data that I can fill into your application so that I can see some results? Peter, I will investigate those links, thanks a lot for them.

Jonny Wray replied on Mon, 2008/07/07 - 10:39am in response to: Geertjan Wielenga

Geertjan,

Not a problem, hope you find it useful. The 'instructions' page on the wiki has an example of running a query and then browsing through the results, including viewing pathway diagrams.

Short version, choose Gene from the search menu and enter say, EPO, in the 'Gene Symbol' field. That'll produce a navigable tree in the tree view. Double clicking on entities with a green arrow icon will then fetch those back. Right click on a pathway entity will bring up a context sensitive menu allow diagram to be displayed.

 Hope that's enough to get you going 

Jonny 

 

 

Geertjan Wielenga replied on Mon, 2008/07/07 - 11:30am

Hi Lieven Doclo, I tried that code for the custom binder and it works perfectly. Thanks for the interesting example.

Gregg Bolinger replied on Mon, 2008/08/11 - 12:49am

Great series!  Any chance an IntelliJ plugin is in the works?  I realize there is a lot of useful context completion availabe currently but it would be nice to have things like view/form/etc beans automatically added to the context file on creation rather than having to do it manually.

Pierre Teddy replied on Fri, 2008/08/15 - 9:25am in response to: Geertjan Wielenga

Great series! should turn it to a book.

Would appreciate some  help with the following problem pleae :

 In the PropertiesExecutor class I am Importing the following Jars

import org.springframework.richclient.command.support.AbstractActionCommandExecutor;
import org.springframework.richclient.dialog.CloseAction;
import org.springframework.richclient.dialog.CompositeDialogPage;
import org.springframework.richclient.dialog.TabbedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;

However I am unable to resolve getWindowControl()

Would you know what I am lacking.

Thanks a lot 

Japan Trivedi replied on Thu, 2009/07/16 - 5:44am

Hi,

I'm new to Spring RCP and I'm trying some demo projects to know more about it. But I want to know that how to build a single executable JAR file for the Spring RCP project. I have tried to execute the test JAR file that is been created in the dist folder of the Net Beans RCP project but it didn't run properly it only shows me the splash screen and then the program ends. Please help me out in this matter.

One more thing I want to know is can we integrate a Spring RCP developed in Net Beans with an applet. Because I need to develop one application in RCP but that needs to be run as a client side applet. Or you can show me some other way.

I regularly refers your tutorial on Spring RCP for NetBeans. And it helps me a lot.

Thanks in advance.

Japan Trivedi,
japan_733@yahoo.co.in

Matt Coleman replied on Mon, 2012/01/16 - 4:41am

it really works!!thank you !! web design buffalo

Mateo Gomez replied on Tue, 2012/01/17 - 1:32am

i love the works you do here netbeans!! keep it up mexican dessert recipes

Steve Sdas replied on Thu, 2012/11/01 - 11:21am

The domain objects will be much more limited than those provided by the example, as will the functionality you will add to the applicationflats for sale in glasgow

Steve Sdas replied on Fri, 2012/11/30 - 7:23am

Thanks for sharing this information its really nice. Internatsgymnasium

Uilly Tocky replied on Tue, 2012/12/04 - 11:34am

Your blogs and every separate material are so remarkable and lucrative it makes me come back most again. Bathroom renovating

Uilly Tocky replied on Wed, 2012/12/05 - 3:06am

I stumbled into this post! and also I’ve bookmarked to appear at unique data for your weblog postSolvarme

Uilly Tocky replied on Fri, 2012/12/07 - 6:59am

This is a great inspiring article.I am pretty much pleased with your good work.You put really helpful information. Keep it up.vivint security reviews

Uilly Tocky replied on Tue, 2012/12/11 - 2:57am

Look forward to exploring your web page repeatedly. I want to encourage that you continue your great job, have a good day.vanessa bruno

Uilly Tocky replied on Tue, 2012/12/11 - 11:30am

Hopefully, you'll have enough information to begin playing with these features yourself!shanghai interior design

Uilly Tocky replied on Thu, 2012/12/13 - 6:19am

Hopefully, you'll have enough information to begin playing with these features yourself.sky blue credit repair review

Uilly Tocky replied on Thu, 2012/12/13 - 8:43am

This was really very interesting and helpful to read. I cant wait to read more from this site. www.ppireclaimco.co.uk

Uilly Tocky replied on Fri, 2012/12/14 - 4:15am

I understand very well. what you have in mind. This is very interestingdwi lawyer in new york city

Uilly Tocky replied on Fri, 2012/12/14 - 6:35am

I really enjoy while I read your blogs and articles. werktisch

Uilly Tocky replied on Mon, 2012/12/17 - 11:15am

This is a very informative and Useful post and help me out to get all valuable information regarding this topic. Great work .Natty

Uilly Tocky replied on Mon, 2012/12/24 - 9:45am

Your site is very informative and your articles are wonderful. psychologist atlanta

Uilly Tocky replied on Wed, 2012/12/26 - 10:27am

I needed to thank you for this excellent infor­mation !! I have book­marked your blog to check out the new stuff you post.The Reeves Law Firm

Uilly Tocky replied on Sat, 2012/12/29 - 9:48am

This article has some great and useful information about this subject. Thank you for sharing it in an easy to read and understandable format.Holidays to Ibiza

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.