Class ChatView

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

public class ChatView extends Container

A scrollable, theme-aware chat surface.

ChatView is a pure UI component -- it has no knowledge of LLMs or any specific chat backend. It accepts ChatMessage instances (the same data envelope the AI client uses, but equally usable for peer-to-peer messaging) and renders them as styled bubbles. Routing the user's input to a backend and piping responses back into the view is the caller's job. For LLM use there's a ready-made binding -- see com.codename1.ai.LlmChatBinding -- but the same surface trivially supports a WhatsApp-style peer chat:

ChatView view = new ChatView();
view.setOnSend(evt -> {
    String text = view.getInput().getText();
    view.getInput().clear();
    view.addMessage(ChatMessage.user(text));          // outgoing
    chatService.send(text, peerReply -> {
        // peer responses render as "assistant" bubbles
        view.addMessage(ChatMessage.assistant(peerReply));
    });
});
// System / "X joined the chat" notices use ChatMessage.system.

The USER / ASSISTANT / SYSTEM roles map naturally to self / peer / system-notice in a peer chat; rename via CSS (the bubble UIIDs are ChatBubbleUser, ChatBubbleAssistant, ChatBubbleSystem) if the visual treatment needs to differ. Apps that need a totally different message data type can subclass and override createBubble(ChatMessage) to render however they like while still using ChatMessage as the transport.

Default UIIDs

ChatView, ChatViewMessages, ChatBubbleUser, ChatBubbleAssistant, ChatBubbleSystem, ChatBubbleText, ChatTypingIndicator, ChatInput, ChatInputField, ChatSendButton, ChatAttachButton, ChatVoiceButton.

  • Constructor Details

    • ChatView

      public ChatView()
  • Method Details

    • addMessage

      public ChatBubble addMessage(ChatMessage message)

      Renders message as a new ChatBubble at the bottom of the list and scrolls into view. Safe to call from any thread.

      The bubble itself is laid out inside a single-row FlowLayout container whose alignment is driven by the message role: USER -> right, ASSISTANT -> left, SYSTEM -> centre. That keeps the bubble's visible width anchored to its text content rather than stretching across the full chat surface, which is what makes a chat bubble actually look like a bubble.

    • beginAssistantStream

      public ChatBubble beginAssistantStream()
      Appends an empty assistant/peer bubble that subsequent ChatBubble.appendText(String) calls (or appendToLastMessage(String)) can stream tokens into. Returns the bubble so the caller can retain the reference -- the typical pattern is to capture it before opening a streaming network call.
    • appendToLastMessage

      public void appendToLastMessage(String delta)
      Append a streaming token delta to the most recently added bubble. Safe to call off-EDT. No-op when there is no bubble yet.
    • setTypingIndicatorVisible

      public void setTypingIndicatorVisible(boolean v)
    • setOnSend

      public void setOnSend(ActionListener listener)
    • setOnAttach

      public void setOnAttach(ActionListener listener)
    • setOnVoice

      public void setOnVoice(ActionListener listener)
    • getInput

      public ChatInput getInput()
    • getHistory

      public List<ChatMessage> getHistory()
    • createBubble

      protected ChatBubble createBubble(ChatMessage message)
      Override to swap in a custom bubble renderer (e.g. one that understands markdown, shows avatars, or formats peer messages differently from LLM responses). Default delegates to ChatBubble.