Class SwingTerminal

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Sized, TerminalExt, Terminal

public class SwingTerminal extends LineDisciplineTerminal
A Swing-based terminal implementation that extends LineDisciplineTerminal.

This class provides a proper JLine Terminal implementation that can be embedded in Swing applications to display a terminal interface. It renders terminal content using Java 2D graphics and handles keyboard and mouse input with proper terminal capabilities.

Features:

  • Full JLine Terminal interface implementation
  • Custom painting for terminal characters and attributes
  • ANSI color support with configurable color palette
  • Font configuration with monospace font support
  • Keyboard input handling with proper terminal capabilities
  • Mouse support for cursor positioning
  • Scrollback buffer support
  • Cursor blinking
  • Constructor Details

    • SwingTerminal

      public SwingTerminal(int columns, int rows) throws IOException
      Creates a new SwingTerminal with the specified dimensions.
      Parameters:
      columns - the number of columns
      rows - the number of rows
      Throws:
      IOException - if an I/O error occurs during initialization
    • SwingTerminal

      public SwingTerminal() throws IOException
      Creates a new SwingTerminal with default dimensions (80x24).
      Throws:
      IOException - if an I/O error occurs during initialization
    • SwingTerminal

      public SwingTerminal(String name, int columns, int rows) throws IOException
      Creates a new SwingTerminal with the specified name and dimensions.
      Parameters:
      name - the terminal name
      columns - the number of columns
      rows - the number of rows
      Throws:
      IOException - if an I/O error occurs during initialization
  • Method Details

    • getComponent

      public SwingTerminal.TerminalComponent getComponent()
      Gets the Swing component that renders the terminal.
      Returns:
      the terminal component
    • createFrame

      public JFrame createFrame(String title)
      Creates a JFrame containing the terminal component.
      Parameters:
      title - the frame title
      Returns:
      the created frame
    • pollInput

      public String pollInput()
      Gets input from the terminal component (non-blocking).
      Returns:
      the next input string, or null if none available
    • takeInput

      public String takeInput() throws InterruptedException
      Gets input from the terminal component (blocking).
      Returns:
      the next input string
      Throws:
      InterruptedException - if interrupted while waiting
    • processInputBytes

      public void processInputBytes(byte[] input) throws IOException
      Processes input bytes through the terminal's line discipline.
      Overrides:
      processInputBytes in class LineDisciplineTerminal
      Parameters:
      input - the input bytes to process
      Throws:
      IOException - if an I/O error occurs
    • write

      public void write(String text)
      Writes text to the terminal component.
      Parameters:
      text - the text to write
    • dump

      public void dump(long[] screen, int x, int y, int height, int width, int[] cursor)
      Dumps the terminal screen data.
      Parameters:
      screen - the screen data array to fill
      x - the starting x coordinate
      y - the starting y coordinate
      height - the height to dump
      width - the width to dump
      cursor - the cursor position array to fill
    • dump

      public String dump(long timeout, boolean forceUpdate) throws InterruptedException
      Dumps the terminal screen data with scrollback.
      Parameters:
      timeout - the number of scrollback lines
      forceUpdate - whether to include scrollback
      Returns:
      the screen data
      Throws:
      InterruptedException - if interrupted while waiting
    • isDirty

      public boolean isDirty()
      Checks if the terminal is dirty (needs repainting).
      Returns:
      true if dirty
    • doClose

      protected void doClose() throws IOException
      Overrides:
      doClose in class LineDisciplineTerminal
      Throws:
      IOException
    • dispose

      @Deprecated public void dispose()
      Deprecated.
      Use AbstractTerminal.close() instead, which properly handles all cleanup including disposing of resources. This method will be made package-private or removed in a future major release.
      Disposes of the terminal resources.
    • setSize

      public void setSize(Sized sz)
      Description copied from interface: Terminal
      Requests that the terminal adopt the specified window dimensions. Implementations may apply constraints or ignore the request if resizing is unsupported; callers should use Terminal.getSize() to observe the effective size after calling this method.
      Specified by:
      setSize in interface Terminal
      Overrides:
      setSize in class LineDisciplineTerminal
      Parameters:
      sz - the desired terminal size (columns and rows)
      See Also: