Class Chip

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

public class Chip extends Container

A small rounded label for a tag, a filter or a choice: Material's input, filter and choice chips.

Selectable, removable and plain chips

// filter chips: tap to toggle
Container filters = FlowLayout.encloseIn(
        new Chip("Vegan").setSelectable(true),
        new Chip("Gluten free").setSelectable(true),
        new Chip("Under 30 min").setSelectable(true).setSelected(true));

// an input chip that can be removed
Chip tag = new Chip("design").setRemovable(true);
tag.addRemoveListener(e -> tag.remove());

A selectable chip toggles on tap and shows a check mark while selected. A removable chip shows a close button, which fires the remove listeners and leaves removing the chip to them. Every tap fires the action listeners. ChipInput is a text field that turns what the user types into chips.

UIIDs: Chip and ChipSelected for the pill, ChipText for its text and ChipRemove for the close button. Unstyled, a chip is a rounded pill in the theme's Slider track colour, and a selected one in the accent colour.

  • Constructor Details

    • Chip

      public Chip(String text)

      A chip showing text.

      Parameters
      • text: the chip's text
  • Method Details

    • getText

      public String getText()

      The chip's text.

      Returns

      the text

    • setText

      public Chip setText(String t)

      Sets the chip's text.

      Parameters
      • t: the text
      Returns

      this, for chaining

    • getTextLabel

      public Button getTextLabel()

      The button holding the text, for an icon or a custom look. It is the chip's lead component, so it receives every tap on the chip.

      Returns

      the text button

    • isSelectable

      public boolean isSelectable()

      Whether a tap toggles the chip.

      Returns

      true for a filter or choice chip

    • setSelectable

      public Chip setSelectable(boolean selectable)

      Makes a tap toggle the chip.

      Parameters
      • selectable: true for a filter or choice chip
      Returns

      this, for chaining

    • isSelected

      public boolean isSelected()

      Whether the chip is selected.

      Returns

      the selection state

    • setSelected

      public Chip setSelected(boolean selected)

      Selects or deselects the chip.

      Parameters
      • selected: the selection state
      Returns

      this, for chaining

    • isRemovable

      public boolean isRemovable()

      Whether the chip shows a close button.

      Returns

      true for an input chip

    • setRemovable

      public Chip setRemovable(boolean removable)

      Shows or hides the close button.

      Parameters
      • removable: true for an input chip
      Returns

      this, for chaining

    • getRemoveButton

      public Button getRemoveButton()

      The close icon. It shows the icon only: taps reach the chip's text button, which removes when the tap lands on this.

      Returns

      the close icon

    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called on every tap.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a tap listener.

      Parameters
      • l: the listener
    • addRemoveListener

      public void addRemoveListener(ActionListener l)

      Adds a listener called when the close button is tapped.

      Parameters
      • l: the listener, which usually removes the chip
    • removeRemoveListener

      public void removeRemoveListener(ActionListener l)

      Removes a close listener.

      Parameters
      • l: the listener
    • pressed

      public void pressed()
      Taps the chip as a user would: toggles it when selectable, then fires the action listeners.
    • requestRemove

      public void requestRemove()
      Fires the remove listeners as the close button would.