Class ChipInput

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class ChipInput extends Container

A text field that turns each entry into a removable Chip: a tag input, a recipient field or a list of keywords.

A recipient field holding three chips

Form form = new Form("Tags", BoxLayout.y());
ChipInput tags = new ChipInput();
tags.setHint("Add a tag");
tags.addValue("design");
tags.addValue("mobile");
tags.addActionListener(e -> Log.p("Tags: " + tags.getValues()));
form.add(tags);
form.show();

An entry becomes a chip when the user presses Done or types a separator (a comma by default, see #setSeparators(String)). Empty entries and, unless #setAllowDuplicates(boolean) says otherwise, repeated ones are ignored. Each chip has a close button that removes it. Action listeners fire after every addition and removal.

The ChipInput is a BorderLayout container whose center holds the chips and the field, so a component added beside them (a "To:" label, say) needs a constraint: add(BorderLayout.WEST, label).

UIIDs: ChipInput for the container and ChipInputField for the text field, falling back to TextField; the chips use the Chip UIIDs.

  • Constructor Details

    • ChipInput

      public ChipInput()
      An empty tag input.
  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • getTextField

      public TextField getTextField()

      The text field the user types into.

      Returns

      the field

    • setHint

      public void setHint(String hint)

      Sets the hint shown in the empty field.

      Parameters
      • hint: the hint
    • getSeparators

      public String getSeparators()

      The characters that end an entry as it is typed.

      Returns

      the separators, "," by default

    • setSeparators

      public void setSeparators(String separators)

      Sets the characters that end an entry as it is typed, such as ", ;" to split on commas, spaces and semicolons. Done always ends one.

      Parameters
      • separators: the separator characters, or "" for Done only
    • isAllowDuplicates

      public boolean isAllowDuplicates()

      Whether the same value can be added twice.

      Returns

      false by default

    • setAllowDuplicates

      public void setAllowDuplicates(boolean allowDuplicates)

      Allows or refuses repeated values. Values compare ignoring case.

      Parameters
      • allowDuplicates: true to allow them
    • getValues

      public List<String> getValues()

      The values, in the order they were added.

      Returns

      a copy of the values

    • addValue

      public boolean addValue(String value)

      Adds a value as a chip, as if the user had typed it.

      Parameters
      • value: the value; surrounding white space is dropped
      Returns

      true if a chip was added, false for an empty or refused duplicate value

    • removeValue

      public boolean removeValue(String value)

      Removes a value and its chip.

      Parameters
      • value: the value, compared ignoring case
      Returns

      true if it was there

    • clearValues

      public void clearValues()
      Removes every value.
    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called after every addition and removal.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • commit

      public void commit()
      Turns the text in the field into a chip, as Done does.