Skip to content
Simmaco Di Maio edited this page Dec 28, 2024 · 1 revision

Is there a way to print Strings an change to the next line using newline characters?

It is possible to handle newline characters (\n) in Jexer to print multi-line strings. While Jexer does not natively support automatically processing newline characters, you can implement this functionality manually by splitting the string based on \n and then printing it line by line.

Here is an example:

public void drawMultilineString(int x, int y, String text, CellAttributes attributes) {
    String[] lines = text.split("\n"); // Split the string based on \n
    for (int i = 0; i < lines.length; i++) {
        putStringXY(x, y + i, lines[i], attributes); // Print each line
    }
}

Usage: Call the drawMultilineString method in the draw() method of your widget or panel:

@Override
public void draw() {
    CellAttributes normal = getTheme().getColor("ttext");
    String text = "Hello, Jexer!\nThis is a new line.\nAnd another one!";
    drawMultilineString(2, 2, text, normal);
}

Behavior: The string is split into separate lines wherever \n is encountered. Each line is printed at the specified x (horizontal) coordinate and an incremented y (vertical) coordinate for each line.

Alternatives: If you're using a widget like TText or TTextBox, you can concatenate multi-line strings and let the widget handle scrolling automatically. However, for manually handling multi-line strings, the method shown above is a simple and effective solution.

package jexer.demos;

import jexer.TApplication;
import jexer.TDesktop;
import jexer.TWindow;
import jexer.bits.CellAttributes;

public class MultilineStringExample extends TWindow {

    public MultilineStringExample(final TApplication application) {
        super(application, "Multiline String Example", 0, 0, 40, 15);
    }

    @Override
    public void draw() {
        // Get the normal text attributes from the theme
        CellAttributes normal = getTheme().getColor("ttext");
        // Multiline text to be drawn
        String text = "Hello, Jexer!\nThis is a new line.\nAnd another one!";
        // Draw the multiline text
        drawMultilineString(2, 2, text, normal);
    }

    /**
     * Draws a multiline string on the screen.
     *
     * @param x          The initial horizontal coordinate.
     * @param y          The initial vertical coordinate.
     * @param text       The string to draw (containing newline `\n`).
     * @param attributes The cell attributes to use.
     */
    public void drawMultilineString(int x, int y, String text, CellAttributes attributes) {
        // Split the string into lines using '\n' as the separator
        String[] lines = text.split("\n");
        for (int i = 0; i < lines.length; i++) {
            putStringXY(x, y + i, lines[i], attributes); // Print each line
        }
    }

    public static void main(String[] args) throws Exception {
        // Launch the application with an XTERM backend
        TApplication app = new TApplication(TApplication.BackendType.XTERM);
        app.addFileMenu();
        // Add a MultilineStringExample window
        MultilineStringExample multilineExample = new MultilineStringExample(app);

        // Run the application
        app.run();
    }
}
Clone this wiki locally