Announcement Announcement Module
No announcement yet.
Binding multiple properties into a single component Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding multiple properties into a single component

    I have multiple checkboxes I would like the GUI to render in a JPanel with a particular layout.

    The builder concept expects a one-to-one relationship between the binding and the resulting JComponent. The JComponent is added to the form individually using the builder.add(propertyName) method. This works fine for simple cases.

    In my situation, I have the same one-to-one relationship between my bindings and the resulting JComponent. The only difference is I want multiple JComponents to be added into a single JPanel, and that JPanel is added to the builder.

    In code, it might look like:
    JPanel wrapper = new JPanel();
    builder.add(new String[] {"prop1", "prop2"}, wrapper);

    Is there any workaround, hack, supported option, hope for this to work?

  • #2
    The thing to remember is that the builder is simply a helper class to help construct the visual layout of forms. Conceivably, it's entirely possible to create the return component of the "createFormControl" method manually - meaning you would manually bind properties to controls via BindingFactory and manually add them to some return component/panel. The form builder is designed to handle the most common cases. The builder also happens to include convenience methods that take care of binding properties to components and then adding them to the form. As things stand now, you will probably have to manually bind those components, build out your JPanel, and then add that to the form, rather than relying on the builder to bind the components. I'm doing this off the top of my head, so I'm not sure of its viability, but the basic idea is:
      SwingBindingFactory bf = (SwingBindingFactory)getBindingFactory();
      TableFormBuilder fb = new TableFormBuilder(bf);
      .. add simple/normal properties using the TableFormBuilder ...
      JPanel panel = new JPanel();
      TableLayoutBuilder tlb = fb.getLayoutBuilder();
      tlb.cell(panel, ... attributes...);
      .. maybe add more simple properties via TableFormBuilder here ...
      return fb.getForm();
    TableFormBuilder uses a TableLayoutBuilder to actually add components and lay them out. I simply extract the TableLayoutBuilder and directly add my custom component, bypassing TableFormBuilder and its need to have a Binding for the component. You may have to look at TableLayoutBuilder for usage (and especially look at how TableFormBuilder uses it). We will probably want an easier way to do this in the future, but for now this should work.

    - Andy


    • #3
      Thanks for the tip Andy. I didn't think of bypassing the builder to go directly to the layout.

      I had previously extended TableFormBuilder to be able to pass a boolean value to all add() methods to determine whether a JLabel should be created or not. Since I had this subclass already, I added an add(JComponent, attributes) method.