Class MentionField

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

public class MentionField extends Container

A text field that suggests people (or tags, or channels) as the user types an @ mention, and completes the name when one is tapped.

A text field suggesting names after an at sign

Form form = new Form("Comment", BoxLayout.y());
MentionField comment = new MentionField();
comment.setCandidates("Ada Lovelace", "Alan Turing", "Grace Hopper");
comment.getTextField().setHint("Write a comment");
form.add(comment);
form.show();
// later
java.util.List<String> notify = comment.getMentions();

The suggestions show under the field, in the layout, while the word at the cursor starts with the trigger (@ by default, see #setTrigger(char)) and matches a candidate: one whose name, or any word of it, starts with what was typed after the trigger, ignoring case. Picking one replaces that word with the trigger, the name and a space. A trigger in the middle of a word, as in an email address, does not start a mention.

For a directory too large to list up front, override #suggest(String).

UIIDs: MentionField for the container, MentionFieldSuggestions for the list and MentionFieldSuggestion for each row, falling back to TextField, Container and a Label look.

  • Constructor Details

    • MentionField

      public MentionField()
      An empty mention field.
  • 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

    • getText

      public String getText()

      The field's text.

      Returns

      the text

    • setText

      public void setText(String text)

      Sets the field's text and updates the suggestions for its last word.

      Parameters
      • text: the text
    • setCandidates

      public void setCandidates(String... names)

      Sets the names offered.

      Parameters
      • names: the candidates
    • getTrigger

      public char getTrigger()

      The character that starts a mention.

      Returns

      @ by default

    • setTrigger

      public void setTrigger(char trigger)

      Sets the character that starts a mention, such as # for tags.

      Parameters
      • trigger: the trigger character
    • setMaxSuggestions

      public void setMaxSuggestions(int max)

      Sets how many suggestions show at most.

      Parameters
      • max: the limit, 5 by default
    • getMentions

      public List<String> getMentions()

      The names the user picked from the suggestions that are still in the text, in the order picked.

      Returns

      the mentioned names

    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called when the user picks a suggestion.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • isShowingSuggestions

      public boolean isShowingSuggestions()

      True while the suggestion list shows.

      Returns

      whether suggestions are visible

    • suggest

      protected List<String> suggest(String query)

      The suggestions for what was typed after the trigger. Override to query a server or a large directory; the default matches the candidates.

      Parameters
      • query: the text after the trigger, possibly empty
      Returns

      the names to offer, best first

    • pick

      public void pick(String name)

      Picks a suggestion as a tap would.

      Parameters
      • name: the name to insert in place of the word being typed