Class ButtonField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.ButtonField

public class ButtonField extends FormField
Represents a FormField that can contain one or more buttons with configurable actions. Internally, the buttons are grouped into a JPanel, which is set as this FormField's fieldComponent. The layout defaults to a FlowLayout. The alignment, hgap, and vgap of the layout can be modified with setAlignment, setHgap, and setVgap methods. Technically, callers can also directly access or even swap out the LayoutManager by talking to the fieldComponent directly, but if you need a highly customized layout, you might be better off using PanelField or CollapsiblePanelField. That way, you have maximum control of the layout of your components.

Validation: by default, ButtonFields do not show a validation label when the form is validated. But if you add a FieldValidator to your ButtonField, then the validation label will appear when the form is validated.

Field label: by default, ButtonFields do not show a field label. You can manually enable a label by calling buttonField.getFieldLabel().setText("My Label").

Since:
swing-extras 2.6
Author:
scorbo2
  • Field Details

  • Constructor Details

    • ButtonField

      public ButtonField()
      Creates an empty ButtonField with no buttons and with a default layout.
    • ButtonField

      public ButtonField(List<Action> actions)
      Creates a ButtonField with the specified actions, each action corresponding to a button. The label for each button is taken from the Action's name property.
      Parameters:
      actions - the list of actions for the buttons
  • Method Details

    • shouldExpand

      public boolean shouldExpand()
      Reports whether this ButtonField should expand to fill available horizontal space.
      Overrides:
      shouldExpand in class FormField
    • setShouldExpand

      public ButtonField setShouldExpand(boolean expand)
      Optionally make this field expand to fill the entire width of the parent FormPanel. Defaults to false.
    • getButtonCount

      public int getButtonCount()
      Returns the count of buttons currently in this ButtonField.
    • getAlignment

      public int getAlignment()
      One of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class.
    • setAlignment

      public ButtonField setAlignment(int alignment)
      One of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class.
    • getHgap

      public int getHgap()
      Gets the horizontal gap between buttons.
    • getVgap

      public int getVgap()
      Gets the vertical gap between button rows.
    • setHgap

      public ButtonField setHgap(int hgap)
      Sets the horizontal gap between buttons, and between buttons and the edge of the containing panel.
    • setVgap

      public ButtonField setVgap(int vgap)
      Sets the vertical gap between buttons, and between buttons and the edge of the containing panel.
    • getButtonPreferredSize

      public Dimension getButtonPreferredSize()
      Gets the preferred size of the buttons in this ButtonField, or null if the buttons should choose their own preferred size.
    • setButtonPreferredSize

      public ButtonField setButtonPreferredSize(Dimension preferredSize)
      Sets the preferred size of the buttons in this ButtonField. If set to null, the buttons will choose their own preferred size.
    • addButton

      public ButtonField addButton(Action action)
      Adds a button with the specified Action to this ButtonField. The action name should be unique!
    • removeButton

      public ButtonField removeButton(String actionName)
      Removes the button with the specified action name from this ButtonField, if such a button exists.
    • setEnabled

      public FormField setEnabled(boolean isEnabled)
      Overridden here so we can enable/disable our buttons as needed.
      Overrides:
      setEnabled in class FormField
      Parameters:
      isEnabled - whether to enable or disable the components.
    • hasValidationLabel

      public boolean hasValidationLabel()
      Overridden here as we generally don't want to show a validation label on a button field. Will return true only if one or more FieldValidators have been explicitly assigned.
      Overrides:
      hasValidationLabel in class FormField