View Full Version : Get lost on how to embed a ComboBox in a Form
linwei
Nov 2nd, 2004, 02:30 PM
I really get lost on how to use comboBox as FormModel in a form. The following is what I want to do, and I believe it's a common task in everyday programing:
I have a class, name it MyClass:
public class MyClass{
public static int final TYPE1=1;
public static int final TYPE1=2;
public static int final TYPE1=3;
private int type;
// other fields....
}
In a Form, I want to display the "type" as a combobox to let the user to choose one. After inspecting several classes on PropertyEditor, PropertyEditorRegistry, ComboBoxAdaptor, ValueModel, FormModel, Form, AbstractForm, DefaultFormModel, I am still lost on how can I get start on implement it.
Keith Donald
Nov 3rd, 2004, 09:57 AM
It's easy :-)
JComboBox typeSelector = getFormModel().createBoundComboBox(
"type", new Object[] { TYPE1, TYPE2, TYPE3 });
This binds the property "type" on the form model's backing form object to the combo box, with a selectable list populated from TYPE1, TYPE2, and TYPE3.
getFormModel() returns an instance of SwingForModel: which is the factory for creating bound form model controls.
linwei
Nov 3rd, 2004, 01:20 PM
Keith,
Thanks a lot, that's beautiful and really help!
Actually, I've reinvented the wheels yesterday: :shock:
I programmed using low level classes such as BeanPropertyAccessStrategy, PropertyAdaptor, PropertyEditorSupport, PropertyEditor, ComboBoxModelAdapter, and with a lot of code I got the same result as yours. I even wanted to post my code in this forum to show off my fruit, but I changed my idea now to avoid misleading others :D
Thanks again springrichclient and your guys excellent work.
linwei
S.J.V. Consultant Ltd.
linwei
Nov 3rd, 2004, 03:35 PM
hi, keith
I followed your code and everything goes fluently, excepting how to display the combobox in the Form. I first tried the following code:
protected JComponent createFormControl() {
FormLayout layout = new FormLayout("left:pref, 5dlu, pref:grow");
BeanFormBuilder formBuilder = new JGoodiesBeanFormBuilder(getFormModel(), layout);
final JComboBox typeSelector = formModel.createBoundComboBox(
"parserId", parserList.toArray());
formBuilder.add("parserId");
return formBuilder.getForm();
}
("parserId" is the property name to be displayed as combobox, "parserList" is the choices list ).
The above code displays "parserId" as textfield, rahter than a combobox. Then I have to register the "typeSelector" as property editor for parserId. The final working code is:
protected JComponent createFormControl() {
FormLayout layout = new FormLayout("left:pref, 5dlu, pref:grow");
BeanFormBuilder formBuilder = new JGoodiesBeanFormBuilder(getFormModel(), layout);
final JComboBox typeSelector = formModel.createBoundComboBox(
"parserId", parserList.toArray());
formModel.registerCustomEditor("parserId", new PropertyEditorSupport(){
public boolean isPaintable() {
return true;
}
public Component getCustomEditor() {
return typeSelector;
}
public boolean supportsCustomEditor() {
return true;
}
});
formBuilder.add("parserId");
return formBuilder.getForm();
}
This displays the parserId as combobox properly. Keith, just wondering whether I go the right way this time? :roll:
oliverhutchison
Nov 3rd, 2004, 04:27 PM
linwei,
It looks to me like you've hit a limitation of JGoodiesBeanFormBuilder which is not as mature as the other 2 form builders that ship with Spring Rich.
If you were to use TableFormBuilder your code would be as follows:
protected JComponent createFormControl() {
TableFormBuilder formBuilder = new TableFormBuilder(formModel);
final JComboBox typeSelector = formModel.createBoundComboBox(
"parserId", null);
formBuilder.add("parserId", typeSelector);
return formBuilder.getForm();
}
I use TableFormBuilder exclusively so I haven't hit this problem before -
we'll need to improve JGoodiesBeanFormBuilder so that it offers more options for binding custom controls. There should certainly be no need to use a property editor for something so simple.
HTH
Ollie
linwei
Nov 3rd, 2004, 04:47 PM
Ollie,
In fact, there is no compelling reason for me to use "JGoodiesFormBuilder" ( just followed the richclientpetclinic demo ). I changed my code to use "TableFormBuilder", it works great!
Hmm, finally, I reduced my LOC from 100 to 10 :twisted:
Keith Donald
Nov 3rd, 2004, 06:23 PM
:oops:
JGoodiesBeanFormBuilder is a weak illustration as is, and should be removed from the sample, replaced with the more extensive TableLayoutBuilder or GridBagLayoutBuilder.
Ollie: if you get a free tick, would you consider doing this? That'd be great! If we feel it is appropriate, we could also obsolete JGoodiesFormBuilder for now (until/if we see the need to build a better builder on top of FormLayout.) But at very least we should get it out of the sample as a best practice, it's really only usable in simple situations...
Keith
oliverhutchison
Nov 3rd, 2004, 06:44 PM
Keith,
I'm happy to switch everthing over to TableFormBuilder I'll probably do it when I get home tonight or tomorow.
As far as obsoleting JGoodiesFormBuilder goes I'd be happy to see the back of it. I find it a real pain trying to work out the colspecs to pass into the FormLayout in fact that's why I wrote TableFormBuilder.
What builders are people using?
Ollie
linwei
Nov 3rd, 2004, 10:09 PM
one suggestion:
I think for a new user like me could possibly run into to same issue as me on how to use a cumstom component as bean property editor. Would it be help if the petclinic sample can also show how to use SwingFormModel.createXXXX, together with TableFormBuilder.add(property, JComponent ).
I am really considering maybe I can make some simple contribution to this project. As my daily work is highly depend on Springframework and Springrc now, I might could help in some way to find/fix bugs, or improve/implement some aspects. Could and how can I make contribution, springrc team?
Keith Donald
Nov 4th, 2004, 07:47 AM
Sure, we happily accept contributions. Submitting patches via JIRA is one way to do it---specifically, let's discuss an enhancement you need, and if we a agree it's a good general fit for the framework, we can identify where it should go and you can submit a patch, which we'll work to integrate. People are already doing this now: I still have official docking integration on my plate! :-)
Chumpski
Nov 16th, 2005, 06:04 AM
protected JComponent createFormControl() {
TableFormBuilder formBuilder = new TableFormBuilder(formModel);
final JComboBox typeSelector = formModel.createBoundComboBox(
"parserId", null);
formBuilder.add("parserId", typeSelector);
return formBuilder.getForm();
}
I want to include a combo box in a form as well. My form Model object doesnt have the 'createBoundComboBox()' method though. In my PreferencesForm class constructor i set my formmodel object like so:
public PreferencesForm() {
super(PreferencesForm.FORM_ID);
prefBean = new PreferencesBean();
setFormModel(FormModelHelper.createUnbufferedFormM odel(prefBean));
//setFormModel(FormModelHelper.createFormModel(prefB ean));
}
prefBean is my own bean class for eventually storing the information returned from the Form. It just contains class members, getters and setters and form rule validation code.
How can i instantiate my FormModel object to be able to use this 'createBoundComboBox()' method? Is their any other way to place a combo box in a form?
Chumpski
Nov 16th, 2005, 06:15 AM
Apologies. I need to use the SwingBindingFactory class to be able to use that handy piece of functionality.:)
agent613
Feb 8th, 2006, 03:53 PM
I have created a ComboBox in this way:
FormModel priceSourceFormModel = FormModelHelper.createFormModel(priceSource);
SwingBindingFactory bindingFactory = new SwingBindingFactory(priceSourceFormModel);
JComboBox comboBox = (JComboBox)bindingFactory.createBoundComboBox("source", new Object[] { PriceSourceHolder.EOD, PriceSourceHolder.HST_CLOSE, PriceSourceHolder.SETTLE, PriceSourceHolder.TRDPRC_1}).getControl();
comboBox.setMaximumSize(comboBox.getPreferredSize( ) );
toolBar.add(comboBox, 4);
The control displays ok. I want to check that the selection gets bound to the bean, so I added the following line to one of my executors (for a toolbar button):
System.out.println("result: " + PriceSourceHolder.getInstance().getSource());
It prints "null", no matter which value I've selected. How can I fix this so that the selection gets bound?
Thanks for your help guys!
MonikaM
Jun 27th, 2008, 09:16 AM
I have to display dealerId list in combo box,but i dont know how to do it.
though with this code==>
formBuilder.addComboBox("selectedDealer","dealersList","name");
i am able to display the list of strings in combo box
but dont know aht to do with this code to disply list of Ids.I used
formBuilder.addComboBox("dealerId","delaersList","name")
it gives BeanCreationException at name.
protected JComponent createFormControl() {
MyTableFormBuilder formBuilder = createFormBuilder();
formBuilder.setFormId( FORM_NAME );
formBuilder.addValidationMessageArea();
formBuilder.row();
formBuilder.add("indId");
formBuilder.addReadOnly("indName");
formBuilder.row();
formBuilder.addComboBox("selectedDealer","dealersList","name");
formBuilder.addReadOnly("dealerId");
formBuilder.row();
return formBuilder.getForm();
}
Please help me to solve this problem.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.