Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Get lost on how to embed a ComboBox in a Form

  1. #1
    Join Date
    Oct 2004
    Location
    Vancouver, Canada
    Posts
    35

    Default Get lost on how to embed a ComboBox in a Form

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    It's easy :-)

    Code:
     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.
    Keith Donald
    Core Spring Development Team

  3. #3
    Join Date
    Oct 2004
    Location
    Vancouver, Canada
    Posts
    35

    Default

    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

    Thanks again springrichclient and your guys excellent work.

    linwei
    S.J.V. Consultant Ltd.

  4. #4
    Join Date
    Oct 2004
    Location
    Vancouver, Canada
    Posts
    35

    Default

    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:
    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:
    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());
            
            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:
    Regards
    linwei

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    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:
    Code:
    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

  6. #6
    Join Date
    Oct 2004
    Location
    Vancouver, Canada
    Posts
    35

    Default

    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:
    Regards
    linwei

  7. #7
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    :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
    Keith Donald
    Core Spring Development Team

  8. #8
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    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

  9. #9
    Join Date
    Oct 2004
    Location
    Vancouver, Canada
    Posts
    35

    Default

    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?
    Regards
    linwei

  10. #10
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    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! :-)
    Keith Donald
    Core Spring Development Team

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 6
    Last Post: Sep 22nd, 2006, 09:08 AM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Replies: 0
    Last Post: Jun 10th, 2005, 08:22 AM
  5. Replies: 3
    Last Post: Jan 26th, 2005, 04:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •