Announcement Announcement Module
Collapse
No announcement yet.
Question regarding code in UI classes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question regarding code in UI classes

    I'm sure this will seem like a silly question after somebody answers it. I'm walking through the PetClinic sample to figure out how the framework operates, and am confused by something I find in the form classes.

    In the following class:
    org.springframework.richclient.samples.petclinic.u i.OwnerGeneralForm

    Code:
        protected JComponent createFormControl() {
            FormLayout layout = new FormLayout("left:pref, 5dlu, pref:grow");
            BeanFormBuilder formBuilder = new JGoodiesBeanFormBuilder(
                    getFormModel(), layout);
            this.firstNameField = formBuilder.add("firstName")[1];
            formBuilder.add("lastName");
            return formBuilder.getForm();
        }
    My question:

    When adding the first field, why is the component in index 1 of the array returned by formBuilder saved to a local variable? It isn't done for subseqeunt text boxes, and from what I can tell the stored value is never used. I commented the code out so that it was:

    Code:
    formBuilder.add("firstName");
    formBuilder.add("lastName");
    return formBuilder.getForm();
    And it seemed to work. Yet, all of the forms follow the first example - including the login form in the security package. I'm sure there must be a reason, I just can't figure it out.

    Help.

    -Dave

  • #2
    When adding the first field, why is the component in index 1 of the array returned by formBuilder saved to a local variable?
    The component in index 1 of the array is the JTextArea created by the FormBuilder for the property "firstName". The "add" methods of BeanFormBuilder always return an array of 2 components; the first being the JLable created for the property the second being input component for the property (a JTextField, JCheckBox, JComboBox etc..) The JTextArea is saved to an instance variable (not a local) so that it can be used to position the cursor when the form gains focus. This is done in the requestFocusInWindow method.

    I'm suprised you didn't get a NPE after you commented out that line though... I'll have to look into that.


    Ollie





    [/code]

    Comment


    • #3
      The JTextArea is saved to an instance variable (not a local) so that it can be used to position the cursor when the form gains focus. This is done in the requestFocusInWindow method.
      Oh, right you are. I guess I would have actually searched to see if the variable was being references. I think the request focus was collapsed for some reason in my IDE, so I didn't see that method call.

      I'm suprised you didn't get a NPE after you commented out that line though... I'll have to look into that.
      I would be too. I tried again, and I did. Obviously an error on my part.

      Thanks for helping clear things up. I knew I would feel stupid afterwards. :oops:

      -Dave

      Comment


      • #4
        Hummm.... I do get the NPE. Are you sure there was no effect when you commented that code out?

        Ollie

        Comment


        • #5
          Not a problem. I should read before posting..

          By the way, I personaly don't like the way the firstName text field always gains focus when the window gains focus so don't feel like that's *the* way to implement a form. I'll suspect we'll improve this in the future.

          Ollie

          Comment


          • #6
            Originally posted by oliverhutchison
            Not a problem. I should read before posting..

            By the way, I personaly don't like the way the firstName text field always gains focus when the window gains focus so don't feel like that's *the* way to implement a form. I'll suspect we'll improve this in the future.
            If one always wanted it to be the first field, wouldn't that method work better in the AbstractForm class? Something like:

            Code:
                public boolean requestFocusInWindow() {
            
                    for &#40;int i = 0; i < getControl&#40;&#41;.getComponents&#40;&#41;.length; i++&#41; &#123;
                        Component c = getControl&#40;&#41;.getComponents&#40;&#41;&#91;i&#93;;
                        if &#40;c.isFocusable&#40;&#41;&#41; &#123; return c.requestFocusInWindow&#40;&#41;; &#125;
                    &#125;
                    return false;
                &#125;
            .. to loop through the components and find the first one eligible for focus? Of course, the isFocusable() method will return the first JLable I think, so some other test would have to go there.

            If something else was being considered, that may not be a good idea. It actually might not be a good idea regardless of any planned changes. It just seemed like the refocus methods were identical in all the examples I saw.

            -Dave

            Comment

            Working...
            X