Class PanelBuilder

java.lang.Object
com.jgoodies.forms.builder.AbstractFormBuilder
com.jgoodies.forms.builder.PanelBuilder
Direct Known Subclasses:
AbstractI15dPanelBuilder, ButtonBarBuilder, ButtonStackBuilder

public class PanelBuilder extends AbstractFormBuilder
An general purpose panel builder that uses the FormLayout to lay out JPanels. It provides convenience methods to set a default border and to add labels, titles and titled separators.

The PanelBuilder is the working horse for layouts when more specialized builders like the ButtonBarBuilder2 or DefaultFormBuilder are inappropriate.

The Forms tutorial includes several examples that present and compare different style to build with the PanelBuilder: static row numbers vs. row variable, explicit CellConstraints vs. builder cursor, static rows vs. dynamically added rows. Also, you may check out the Tips & Tricks section of the Forms HTML documentation.

The text arguments passed to the methods #addLabel, #addTitle, and #addSeparator can contain an optional mnemonic marker. The mnemonic and mnemonic index are indicated by a single ampersand (&). For example "&Saveinvalid input: '&quot', or "Save &asinvalid input: '&quot'. To use the ampersand itself duplicate it, for example "Look&&Feelinvalid input: '&quot'.

Example:
This example creates a panel with 3 columns and 3 rows.

 FormLayout layout = new FormLayout(
      "pref, $lcgap, 50dlu, $rgap, default",  // columns
      "pref, $lg, pref, $lg, pref");          // rows

 PanelBuilder builder = new PanelBuilder(layout);
 CellConstraints cc = new CellConstraints();
 builder.addLabel("invalid input: '&Title':",        cc.xy  (1, 1));
 builder.add(new JTextField(),      cc.xywh(3, 1, 3, 1));
 builder.addLabel("invalid input: '&Price':",        cc.xy  (1, 3));
 builder.add(new JTextField(),      cc.xy  (3, 3));
 builder.addLabel("invalid input: '&Author':",       cc.xy  (1, 5));
 builder.add(new JTextField(),      cc.xy  (3, 5));
 builder.add(new JButton("?"), cc.xy  (5, 5));
 return builder.getPanel();
 
Version:
$Revision: 1.15 $
Author:
Karsten Lentzsch
See Also:
  • Constructor Details

    • PanelBuilder

      public PanelBuilder(FormLayout layout)
      Constructs a PanelBuilder for the given layout. Uses an instance of JPanel as layout container with the given layout as layout manager.
      Parameters:
      layout - the FormLayout to use
    • PanelBuilder

      public PanelBuilder(FormLayout layout, JPanel panel)
      Constructs a PanelBuilder for the given FormLayout and layout container.
      Parameters:
      layout - the FormLayout to use
      panel - the layout container to build on
  • Method Details

    • getLabelForFeatureEnabledDefault

      public static boolean getLabelForFeatureEnabledDefault()
      Returns the global default for the enablement of the setLabelFor feature. This can be overridden per PanelBuilder using setLabelForFeatureEnabled(boolean). The feature is globally disabled by default.
      Returns:
      true for globally enabled, false for globally disabled
    • setLabelForFeatureEnabledDefault

      public static void setLabelForFeatureEnabledDefault(boolean b)
      Sets the default value for the setLabelFor feature enablement. This can be overridden per PanelBuilder using setLabelForFeatureEnabled(boolean). The default value is used to set the initial PanelBuilder setting for this feature. The feature is globally disabled by default.
      Parameters:
      b - true for globally enabled, false for globally disabled
    • isLabelForFeatureEnabled

      public boolean isLabelForFeatureEnabled()
      Returns whether the setLabelFor feature is enabled for this PanelBuilder. The value is initialized from the global default value for this feature getLabelForFeatureEnabledDefault(). It is globally disabled by default.
      Returns:
      true for enabled, false for disabled
    • setLabelForFeatureEnabled

      public void setLabelForFeatureEnabled(boolean b)
      Enables or disables the setLabelFor feature for this PanelBuilder. The value is initialized from the global default value getLabelForFeatureEnabledDefault(). It is globally disabled by default.
      Parameters:
      b - true for enabled, false for disabled
    • getPanel

      public final JPanel getPanel()
      Returns the panel used to build the form.
      Returns:
      the panel used by this builder to build the form
    • setBackground

      public final void setBackground(Color background)
      Sets the panel's background color.
      Parameters:
      background - the color to set as new background
      Since:
      1.1
      See Also:
    • setBorder

      public final void setBorder(Border border)
      Sets the panel's border.
      Parameters:
      border - the border to set
      See Also:
    • setDefaultDialogBorder

      public final void setDefaultDialogBorder()
      Sets the default dialog border.
      See Also:
    • setOpaque

      public final void setOpaque(boolean b)
      Sets the panel's opaque state.
      Parameters:
      b - true for opaque, false for non-opaque
      Since:
      1.1
      See Also:
    • addLabel

      public final JLabel addLabel(String textWithMnemonic)
      Adds a textual label to the form using the default constraints.

       addLabel("Name:");       // No Mnemonic
       addLabel("Ninvalid input: '&ame':");      // Mnemonic is 'a'
       addLabel("Save invalid input: '&as':");   // Mnemonic is the second 'a'
       addLabel("Lookinvalid input: '&'invalid input: '&Feel':"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      Returns:
      the new label
      See Also:
    • addLabel

      public final JLabel addLabel(String textWithMnemonic, CellConstraints constraints)
      Adds a textual label to the form using the specified constraints.

       addLabel("Name:",       cc.xy(1, 1)); // No Mnemonic
       addLabel("Ninvalid input: '&ame':",      cc.xy(1, 1)); // Mnemonic is 'a'
       addLabel("Save invalid input: '&as':",   cc.xy(1, 1)); // Mnemonic is the second 'a'
       addLabel("Lookinvalid input: '&'invalid input: '&Feel':", cc.xy(1, 1)); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      constraints - the label's cell constraints
      Returns:
      the new label
      See Also:
    • addLabel

      public final JLabel addLabel(String textWithMnemonic, String encodedConstraints)
      Adds a textual label to the form using the specified constraints.

       addLabel("Name:",       "1, 1"); // No Mnemonic
       addLabel("Ninvalid input: '&ame':",      "1, 1"); // Mnemonic is 'a'
       addLabel("Save invalid input: '&as':",   "1, 1"); // Mnemonic is the second 'a'
       addLabel("Lookinvalid input: '&'invalid input: '&Feel':", "1, 1"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      encodedConstraints - a string representation for the constraints
      Returns:
      the new label
      See Also:
    • addLabel

      public final JLabel addLabel(String textWithMnemonic, CellConstraints labelConstraints, Component component, CellConstraints componentConstraints)
      Adds a label and component to the panel using the given cell constraints. Sets the given label as the component label using JLabel.setLabelFor(java.awt.Component).

      Note: The CellConstraints objects for the label and the component must be different. Cell constraints are implicitly cloned by the FormLayout when added to the container. However, in this case you may be tempted to reuse a CellConstraints object in the same way as with many other builder methods that require a single CellConstraints parameter. The pitfall is that the methods CellConstraints.xy*(...) just set the coordinates but do not create a new instance. And so the second invocation of xy*(...) overrides the settings performed in the first invocation before the object is cloned by the FormLayout.

      Wrong:

       builder.addLabel(
           "invalid input: '&Name':",            // Mnemonic is 'N'
           cc.xy(1, 7),         // will be modified by the code below
           nameField,
           cc.xy(3, 7)          // sets the single instance to (3, 7)
       );
       
      Correct:
       // Using a single CellConstraints instance and cloning
       CellConstraints cc = new CellConstraints();
       builder.addLabel(
           "invalid input: '&Name':",
           (CellConstraints) cc.xy(1, 7).clone(), // cloned before the next modification
           nameField,
           cc.xy(3, 7)                            // sets this instance to (3, 7)
       );
      
       // Using two CellConstraints instances
       CellConstraints cc1 = new CellConstraints();
       CellConstraints cc2 = new CellConstraints();
       builder.addLabel(
           "invalid input: '&Name':",           // Mnemonic is 'N'
           cc1.xy(1, 7),       // sets instance 1 to (1, 7)
           nameField,
           cc2.xy(3, 7)        // sets instance 2 to (3, 7)
       );
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      labelConstraints - the label's cell constraints
      component - the component to add
      componentConstraints - the component's cell constraints
      Returns:
      the added label
      Throws:
      IllegalArgumentException - if the same cell constraints instance is used for the label and the component
      See Also:
    • addROLabel

      public final JLabel addROLabel(String textWithMnemonic)
      Adds a textual label intended for labeling read-only components to the form using the default constraints.

       addROLabel("Name:");       // No Mnemonic
       addROLabel("Ninvalid input: '&ame':");      // Mnemonic is 'a'
       addROLabel("Save invalid input: '&as':");   // Mnemonic is the second 'a'
       addROLabel("Lookinvalid input: '&'invalid input: '&Feel':"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      Returns:
      the new label
      Since:
      1.3
      See Also:
    • addROLabel

      public final JLabel addROLabel(String textWithMnemonic, CellConstraints constraints)
      Adds a textual label intended for labeling read-only components to the form using the specified constraints.

       addROLabel("Name:",       cc.xy(1, 1)); // No Mnemonic
       addROLabel("Ninvalid input: '&ame':",      cc.xy(1, 1)); // Mnemonic is 'a'
       addROLabel("Save invalid input: '&as':",   cc.xy(1, 1)); // Mnemonic is the second 'a'
       addROLabel("Lookinvalid input: '&'invalid input: '&Feel':", cc.xy(1, 1)); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      constraints - the label's cell constraints
      Returns:
      the new label
      Since:
      1.3
      See Also:
    • addROLabel

      public final JLabel addROLabel(String textWithMnemonic, String encodedConstraints)
      Adds a textual label intended for labeling read-only components to the form using the specified constraints.

       addROLabel("Name:",       "1, 1"); // No Mnemonic
       addROLabel("Ninvalid input: '&ame':",      "1, 1"); // Mnemonic is 'a'
       addROLabel("Save invalid input: '&as':",   "1, 1"); // Mnemonic is the second 'a'
       addROLabel("Lookinvalid input: '&'invalid input: '&Feel':", "1, 1"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      encodedConstraints - a string representation for the constraints
      Returns:
      the new label
      Since:
      1.3
      See Also:
    • addROLabel

      public final JLabel addROLabel(String textWithMnemonic, CellConstraints labelConstraints, Component component, CellConstraints componentConstraints)
      Adds a label and component to the panel using the given cell constraints. Sets the given label as the component label using JLabel.setLabelFor(java.awt.Component).

      Note: The CellConstraints objects for the label and the component must be different. Cell constraints are implicitly cloned by the FormLayout when added to the container. However, in this case you may be tempted to reuse a CellConstraints object in the same way as with many other builder methods that require a single CellConstraints parameter. The pitfall is that the methods CellConstraints.xy*(...) just set the coordinates but do not create a new instance. And so the second invocation of xy*(...) overrides the settings performed in the first invocation before the object is cloned by the FormLayout.

      Wrong:

       builder.addROLabel(
           "invalid input: '&Name':",            // Mnemonic is 'N'
           cc.xy(1, 7),         // will be modified by the code below
           nameField,
           cc.xy(3, 7)          // sets the single instance to (3, 7)
       );
       
      Correct:
       // Using a single CellConstraints instance and cloning
       CellConstraints cc = new CellConstraints();
       builder.addROLabel(
           "invalid input: '&Name':",
           (CellConstraints) cc.xy(1, 7).clone(), // cloned before the next modification
           nameField,
           cc.xy(3, 7)                            // sets this instance to (3, 7)
       );
      
       // Using two CellConstraints instances
       CellConstraints cc1 = new CellConstraints();
       CellConstraints cc2 = new CellConstraints();
       builder.addROLabel(
           "invalid input: '&Name':",           // Mnemonic is 'N'
           cc1.xy(1, 7),       // sets instance 1 to (1, 7)
           nameField,
           cc2.xy(3, 7)        // sets instance 2 to (3, 7)
       );
       
      Parameters:
      textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
      labelConstraints - the label's cell constraints
      component - the component to add
      componentConstraints - the component's cell constraints
      Returns:
      the added label
      Throws:
      IllegalArgumentException - if the same cell constraints instance is used for the label and the component
      Since:
      1.3
      See Also:
    • addTitle

      public final JLabel addTitle(String textWithMnemonic)
      Adds a title label to the form using the default constraints.

       addTitle("Name");       // No mnemonic
       addTitle("Ninvalid input: '&ame'");      // Mnemonic is 'a'
       addTitle("Save invalid input: '&as'");   // Mnemonic is the second 'a'
       addTitle("Lookinvalid input: '&'invalid input: '&Feel'"); // No mnemonic, text is Lookinvalid input: '&Feel'
       
      Parameters:
      textWithMnemonic - the title label's text - may contain an ampersand (&) to mark a mnemonic
      Returns:
      the added title label
      See Also:
    • addTitle

      public final JLabel addTitle(String textWithMnemonic, CellConstraints constraints)
      Adds a title label to the form using the specified constraints.

       addTitle("Name",       cc.xy(1, 1)); // No mnemonic
       addTitle("Ninvalid input: '&ame'",      cc.xy(1, 1)); // Mnemonic is 'a'
       addTitle("Save invalid input: '&as'",   cc.xy(1, 1)); // Mnemonic is the second 'a'
       addTitle("Lookinvalid input: '&'invalid input: '&Feel'", cc.xy(1, 1)); // No mnemonic, text is Lookinvalid input: '&Feel'
       
      Parameters:
      textWithMnemonic - the title label's text - may contain an ampersand (&) to mark a mnemonic
      constraints - the separator's cell constraints
      Returns:
      the added title label
      See Also:
    • addTitle

      public final JLabel addTitle(String textWithMnemonic, String encodedConstraints)
      Adds a title label to the form using the specified constraints.

       addTitle("Name",       "1, 1"); // No mnemonic
       addTitle("Ninvalid input: '&ame'",      "1, 1"); // Mnemonic is 'a'
       addTitle("Save invalid input: '&as'",   "1, 1"); // Mnemonic is the second 'a'
       addTitle("Lookinvalid input: '&'invalid input: '&Feel'", "1, 1"); // No mnemonic, text is Lookinvalid input: '&Feel'
       
      Parameters:
      textWithMnemonic - the title label's text - may contain an ampersand (&) to mark a mnemonic
      encodedConstraints - a string representation for the constraints
      Returns:
      the added title label
      See Also:
    • addSeparator

      public final JComponent addSeparator(String textWithMnemonic)
      Adds a titled separator to the form that spans all columns.

       addSeparator("Name");       // No Mnemonic
       addSeparator("Ninvalid input: '&ame'");      // Mnemonic is 'a'
       addSeparator("Save invalid input: '&as'");   // Mnemonic is the second 'a'
       addSeparator("Lookinvalid input: '&'invalid input: '&Feel'"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the separator label's text - may contain an ampersand (&) to mark a mnemonic
      Returns:
      the added separator
    • addSeparator

      public final JComponent addSeparator(String textWithMnemonic, CellConstraints constraints)
      Adds a titled separator to the form using the specified constraints.

       addSeparator("Name",       cc.xy(1, 1)); // No Mnemonic
       addSeparator("Ninvalid input: '&ame'",      cc.xy(1, 1)); // Mnemonic is 'a'
       addSeparator("Save invalid input: '&as'",   cc.xy(1, 1)); // Mnemonic is the second 'a'
       addSeparator("Lookinvalid input: '&'invalid input: '&Feel'", cc.xy(1, 1)); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the separator label's text - may contain an ampersand (&) to mark a mnemonic
      constraints - the separator's cell constraints
      Returns:
      the added separator
    • addSeparator

      public final JComponent addSeparator(String textWithMnemonic, String encodedConstraints)
      Adds a titled separator to the form using the specified constraints.

       addSeparator("Name",       "1, 1"); // No Mnemonic
       addSeparator("Ninvalid input: '&ame'",      "1, 1"); // Mnemonic is 'a'
       addSeparator("Save invalid input: '&as'",   "1, 1"); // Mnemonic is the second 'a'
       addSeparator("Lookinvalid input: '&'invalid input: '&Feel'", "1, 1"); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the separator label's text - may contain an ampersand (&) to mark a mnemonic
      encodedConstraints - a string representation for the constraints
      Returns:
      the added separator
    • addSeparator

      public final JComponent addSeparator(String textWithMnemonic, int columnSpan)
      Adds a titled separator to the form that spans the specified columns.

       addSeparator("Name",       3); // No Mnemonic
       addSeparator("Ninvalid input: '&ame'",      3); // Mnemonic is 'a'
       addSeparator("Save invalid input: '&as'",   3); // Mnemonic is the second 'a'
       addSeparator("Lookinvalid input: '&'invalid input: '&Feel'", 3); // No mnemonic, text is "lookinvalid input: '&feel'"
       
      Parameters:
      textWithMnemonic - the separator label's text - may contain an ampersand (&) to mark a mnemonic
      columnSpan - the number of columns the separator spans
      Returns:
      the added separator
    • add

      public final JLabel add(JLabel label, CellConstraints labelConstraints, Component component, CellConstraints componentConstraints)
      Adds a label and component to the panel using the given cell constraints. Sets the given label as the component label using JLabel.setLabelFor(java.awt.Component).

      Note: The CellConstraints objects for the label and the component must be different. Cell constraints are implicitly cloned by the FormLayout when added to the container. However, in this case you may be tempted to reuse a CellConstraints object in the same way as with many other builder methods that require a single CellConstraints parameter. The pitfall is that the methods CellConstraints.xy*(...) just set the coordinates but do not create a new instance. And so the second invocation of xy*(...) overrides the settings performed in the first invocation before the object is cloned by the FormLayout.

      Wrong:

       CellConstraints cc = new CellConstraints();
       builder.add(
           nameLabel,
           cc.xy(1, 7),         // will be modified by the code below
           nameField,
           cc.xy(3, 7)          // sets the single instance to (3, 7)
       );
       
      Correct:
       // Using a single CellConstraints instance and cloning
       CellConstraints cc = new CellConstraints();
       builder.add(
           nameLabel,
           (CellConstraints) cc.xy(1, 7).clone(), // cloned before the next modification
           nameField,
           cc.xy(3, 7)                            // sets this instance to (3, 7)
       );
      
       // Using two CellConstraints instances
       CellConstraints cc1 = new CellConstraints();
       CellConstraints cc2 = new CellConstraints();
       builder.add(
           nameLabel,
           cc1.xy(1, 7),       // sets instance 1 to (1, 7)
           nameField,
           cc2.xy(3, 7)        // sets instance 2 to (3, 7)
       );
       
      Parameters:
      label - the label to add
      labelConstraints - the label's cell constraints
      component - the component to add
      componentConstraints - the component's cell constraints
      Returns:
      the added label
      Throws:
      IllegalArgumentException - if the same cell constraints instance is used for the label and the component
      See Also:
    • getComponentFactory

      public final ComponentFactory getComponentFactory()
      Returns the builder's component factory. If no factory has been set before, it is lazily initialized using with an instance of DefaultComponentFactory.
      Returns:
      the component factory
      See Also:
    • setComponentFactory

      public final void setComponentFactory(ComponentFactory newFactory)
      Sets a new component factory.
      Parameters:
      newFactory - the component factory to be set
      See Also:
    • add

      public Component add(Component component, CellConstraints cellConstraints)
      Adds a component to the panel using the given cell constraints. In addition to the superclass behavior, this implementation tracks the most recently added label, and associates it with the next added component that is applicable for being set as component for the label.
      Overrides:
      add in class AbstractFormBuilder
      Parameters:
      component - the component to add
      cellConstraints - the component's cell constraints
      Returns:
      the added component
      See Also:
    • isLabelForApplicable

      protected boolean isLabelForApplicable(JLabel label, Component component)
      Checks and answers whether the given component shall be set as component for a previously added label using JLabel.setLabelFor(Component). This default implementation checks whether the component is focusable, and - if a JComponent - whether it is already labeled by a JLabel. Subclasses may override.
      Parameters:
      label - the candidate for labeling component
      component - the component that could be labeled by label
      Returns:
      true if focusable, false otherwise
    • setLabelFor

      protected void setLabelFor(JLabel label, Component component)
      Sets label as labeling label for component or an appropriate child. In case of a JScrollPane as given component, this default implementation labels the view of the scroll pane's viewport.
      Parameters:
      label - the labeling label
      component - the component to be labeled, or the parent of the labeled component