Class SwingValidationGroup


public final class SwingValidationGroup extends ValidationGroup
ValidationGroup subclass specialized for handling Swing components. This subclass has add-methods for adding GUI-components for common Swing cases. There are also a method for getting the SwingComponentDecorationFactory used by this SwingValidationGroup to create decorations for the separate GUI-components added to the group. A custom SwingComponentDecorationFactory can be specified when creating the SwingValidationGroup.

For components this library supports out-of-the-box such as JTextFields or JComboBoxes, simply call one of the add() methods with your component and validators. For validating your own components or ones this class doesn't have methods for, you implement ValidationListeners, and add them to the ValidationGroup using the the method ValidationGroup.addItem(org.netbeans.validation.api.ui.ValidationItem, boolean)

  • Method Details

    • create

      public static SwingValidationGroup create(ValidationUI... ui)
    • create

      public static SwingValidationGroup create(GroupValidator additionalGroupValidation, ValidationUI... ui)
      Creates a SwingValidationGroup. Will use a SwingComponentDecorationFactory returned by SwingComponentDecorationFactory.getDefault() to modify the appearance of subsequently added components (to show that there is a problem with a component's content). To instead use a custom SwingComponentDecorationFactory, call create(org.netbeans.validation.api.ui.GroupValidator, org.netbeans.validation.api.ui.swing.SwingComponentDecorationFactory, org.netbeans.validation.api.ui.ValidationUI[])
      Parameters:
      ui - Zero or more ValidationUI:s. Will be used by the SwingValidationGroup to show the leading problem (if any)
    • create

      public static SwingValidationGroup create(GroupValidator additionalGroupValidation, SwingComponentDecorationFactory decorator, ValidationUI... ui)
      Creates a SwingValidationGroup.
      Parameters:
      additionalGroupValidation - may be null
      decorator - A decorator to be used to modify the appearance of subsequently added components (to show that there is a problem with a component's content).
      ui - Zero or more ValidationUI:s. Will all be used by the SwingValidationGroup to show the leading problem (if any)
    • decorationFor

      protected final <T> ValidationUI decorationFor(T comp)
      Overrides:
      decorationFor in class ValidationGroup
    • add

      @SafeVarargs public final void add(JTextComponent comp, Validator<String>... validators)
      Add a text component to be validated using the passed validators.

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Swing Documents (the model used by JTextComponent) are thread-safe, and can be modified from other threads. In the case that a text component validator receives an event on another thread, validation will be scheduled for later, on the event thread.

      Parameters:
      comp - A text component such as a JTextField
      validators - One or more Validators
    • add

      public final void add(JTextComponent comp, Validator<String> validator)
      Add a text component to be validated using the passed validator.

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Swing Documents (the model used by JTextComponent) are thread-safe, and can be modified from other threads. In the case that a text component validator receives an event on another thread, validation will be scheduled for later, on the event thread.

      Unlike add(JTextComponent,Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to add more than one validator, simply add the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      comp - A text component such as a JTextField
      validator - a validator
    • add

      public final void add(JComboBox box, Validator<String>... validators)
      Add a combo box to be validated using the passed validators

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Parameters:
      box - A combo box component
      validators - One or more Validators
    • add

      public final void add(JComboBox box, Validator<String> validator)
      Add a combo box to be validated using the passed validator.

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Unlike add(JComboBox,Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to add more than one validator, simply add the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      box - A combo box component
      validator - a validator
    • add

      public final void add(JList list, Validator<Integer[]>... validators)
      Add a JList to be validated using the passed validators

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Parameters:
      list - A JList component
      validators - One or more Validators
    • add

      public final void add(JList list, Validator<Integer[]> validator)
      Add a JList to be validated using the passed validator.

      When a problem occurs, the created ValidationListener will use a ValidationUI created by this ValidationGroup to decorate the component.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Unlike add(JList,Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to add more than one validator, simply add the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      list - A JList component
      validator - a validator
    • add

      public final void add(AbstractButton[] buttons, Validator<Integer[]>... validators)
      Add a validator of button models - typically to see if any are selected.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Parameters:
      buttons - The buttons
      validators - A number of Validators
    • add

      public final void add(AbstractButton[] buttons, Validator<Integer[]> validator)
      Add a validator of button models - typically to see if any are selected.

      Note: All methods in this class must be called from the AWT Event Dispatch thread, or assertion errors will be thrown. Manipulating components on other threads is not safe.

      Unlike add(AbstractButton[],Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to add more than one validator, simply add the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      buttons - The buttons
      validator - a validator
    • createProblemLabel

      public final JComponent createProblemLabel()
      Create a label which will show the current problem if any, which can be added to a panel that uses validation
      Returns:
      A JLabel
    • nameForComponent

      public static String nameForComponent(JComponent jc)
      Get a string name for a component using the following strategy:
      1. Check jc.getClientProperty(CLIENT_PROP_NAME)
      2. If that returned null, call jc.getName()
      Parameters:
      jc - The component
      Returns:
      its name, if any, or null
    • setComponentName

      public static void setComponentName(JComponent comp, String localizedName)