Skip to content

StyledText Examples

A StyledText is an editable user interface object that displays lines of text.

Snippet 1

StyledText  Snippet 1

Demonstrates basic text styling with SWT StyledText widget.

This example shows how to apply different text styles to different portions of text:

  • Bold formatting for the first 10 characters

  • Red foreground color for the middle section

  • Blue background color for the last section

Each style range is applied using StyleRange objects with specific start positions and lengths.

StyledTextSnippet1.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates basic text styling with SWT StyledText widget.
 * This example shows how to apply different text styles to different portions of text:
 * - Bold formatting for the first 10 characters
 * - Red foreground color for the middle section
 * - Blue background color for the last section
 * Each style range is applied using StyleRange objects with specific start positions and lengths.
 */
public class StyledTextSnippet1 {

public static void main(String[] args) throws ClassNotFoundException {
    Config.useEquo(StyledText.class);
    Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
    Config.useEquo(StyleRange.class);

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText Snippet 1");
    shell.setLayout(new FillLayout());
    shell.setSize(400, 100);
    StyledText text = new StyledText (shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.fontStyle = SWT.BOLD;
    text.setStyleRange(style1);
    StyleRange style2 = new StyleRange();
    style2.start = 11;
    style2.length = 13;
    style2.foreground = display.getSystemColor(SWT.COLOR_RED);
    text.setStyleRange(style2);
    StyleRange style3 = new StyleRange();
    style3.start = 25;
    style3.length = 13;
    style3.background = display.getSystemColor(SWT.COLOR_BLUE);
    text.setStyleRange(style3);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
}

View on GitHub

Snippet 2

StyledText  Snippet 2

Demonstrates text paragraph formatting with SWT StyledText widget.

This example shows different paragraph-level formatting options:

  • First paragraph: 50-pixel indentation from the left margin

  • Second paragraph: Center alignment

  • Third paragraph: Justified text alignment (text aligned to both margins)

The text content explains each formatting feature while demonstrating it visually.

StyledTextSnippet2.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates text paragraph formatting with SWT StyledText widget.
 * This example shows different paragraph-level formatting options:
 * - First paragraph: 50-pixel indentation from the left margin
 * - Second paragraph: Center alignment
 * - Third paragraph: Justified text alignment (text aligned to both margins)
 * The text content explains each formatting feature while demonstrating it visually.
 */
public class StyledTextSnippet2 {

    static String text =
        "The first paragraph has an indentation of fifty pixels. Indentation is the amount of white space in front of the first line of a paragraph.\n\n" +
        "The second paragraph is center aligned. Alignment, as with all other line attributes, can be set for the whole widget or just for a set of lines.\n\n" +
        "The third paragraph is justified, which means the text is aligned to both the left and right margins. This creates a uniform appearance by evenly distributing spaces between words.";

    public static void main(String [] args) throws ClassNotFoundException {
        Config.useEquo(StyledText.class);
        Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
        Config.useEquo(StyleRange.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("StyledText Snippet 2");
        shell.setLayout(new FillLayout());
        StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
        styledText.setText(text);
        styledText.setLineIndent(0, 1, 50);
        styledText.setLineAlignment(2, 1, SWT.CENTER);
        styledText.setLineJustify(4, 1, true);

        shell.setSize(300, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

View on GitHub

Snippet 3

StyledText  Snippet 3

Combines character-level styling with paragraph-level formatting in SWT StyledText.

This example demonstrates both types of formatting applied together:

Character-level styling:

  • First paragraph: Bold font style applied to specific text range

  • Second paragraph: Red foreground color applied to specific text range

  • Third paragraph: Blue background color applied to specific text range

Paragraph-level formatting:

  • First paragraph: 50-pixel left indentation

  • Second paragraph: Center alignment

  • Third paragraph: Justified alignment

StyledTextSnippet3.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Combines character-level styling with paragraph-level formatting in SWT StyledText.
 * This example demonstrates both types of formatting applied together:
 * Character-level styling:
 * - First paragraph: Bold font style applied to specific text range
 * - Second paragraph: Red foreground color applied to specific text range  
 * - Third paragraph: Blue background color applied to specific text range
 * Paragraph-level formatting:
 * - First paragraph: 50-pixel left indentation
 * - Second paragraph: Center alignment
 * - Third paragraph: Justified alignment
 */
public class StyledTextSnippet3 {
    static String text =
            "This paragraph has an indentation of fifty pixels and bold font style applied.\n\n" +
                    "This paragraph is center aligned and has red foreground color.\n\n" +
                    "This paragraph is justified and has blue background color.";

    public static void main(String [] args) throws ClassNotFoundException {
        Config.useEquo(StyledText.class);
        Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
        Config.useEquo(StyleRange.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("StyledText Snippet 3");
        shell.setLayout(new FillLayout());
        StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
        styledText.setText(text);

        StyleRange style1 = new StyleRange();
        style1.start = 0;
        style1.length = 78;
        style1.fontStyle = SWT.BOLD;
        styledText.setStyleRange(style1);

        StyleRange style2 = new StyleRange();
        style2.start = 80;
        style2.length = 62;
        style2.foreground = display.getSystemColor(SWT.COLOR_RED);
        styledText.setStyleRange(style2);

        StyleRange style3 = new StyleRange();
        style3.start = 144;
        style3.length = 58;
        style3.background = display.getSystemColor(SWT.COLOR_BLUE);
        styledText.setStyleRange(style3);

        styledText.setLineIndent(0, 1, 50);
        styledText.setLineAlignment(2, 1, SWT.CENTER);
        styledText.setLineJustify(4, 1, true);

        shell.setSize(300, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

View on GitHub

Snippet 4

StyledText  Snippet 4

Demonstrates an editable StyledText widget with user interaction capabilities.

This example shows:

  • A StyledText widget that allows user input and text editing

  • Bold formatting applied to the entire initial text content

  • Interactive text editing where users can click anywhere to position the caret

  • Full editing capabilities including typing, deleting, and modifying text

The text content explains its own interactive features to guide user interaction.

StyledTextSnippet4.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates an editable StyledText widget with user interaction capabilities.
 * This example shows:
 * - A StyledText widget that allows user input and text editing
 * - Bold formatting applied to the entire initial text content
 * - Interactive text editing where users can click anywhere to position the caret
 * - Full editing capabilities including typing, deleting, and modifying text
 * The text content explains its own interactive features to guide user interaction.
 */
public class StyledTextSnippet4 {

public static void main(String[] args) throws ClassNotFoundException {
    Config.useEquo(StyledText.class);
    Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
    Config.useEquo(StyleRange.class);

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText Snippet 4");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText (shell, SWT.BORDER);
    text.setText("This text is editable. You may click at any part of it to change caret location and start modifying its content");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = text.getCharCount();
    style1.fontStyle = SWT.BOLD;
    text.setStyleRange(style1);

    shell.setSize(500, 80);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
}

View on GitHub

Snippet 5

StyledText  Snippet 5

Demonstrates a read-only StyledText widget that prevents user editing.

This example shows:

  • A StyledText widget with editing disabled using setEditable(false)

  • Bold formatting applied to the entire text content

  • Text content that explains its read-only nature

  • Users can still select text and navigate with the cursor, but cannot modify the content

Useful for displaying formatted text that should not be editable, like help text or status messages.

StyledTextSnippet5.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates a read-only StyledText widget that prevents user editing.
 * This example shows:
 * - A StyledText widget with editing disabled using setEditable(false)
 * - Bold formatting applied to the entire text content
 * - Text content that explains its read-only nature
 * - Users can still select text and navigate with the cursor, but cannot modify the content
 * Useful for displaying formatted text that should not be editable, like help text or status messages.
 */
public class StyledTextSnippet5 {

public static void main(String[] args) throws ClassNotFoundException {
    Config.useEquo(StyledText.class);
    Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
    Config.useEquo(StyleRange.class);

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText Snippet 5");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText (shell, SWT.BORDER);
    text.setText("This text is not editable, which means you can't modify its content");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = text.getCharCount();
    style1.fontStyle = SWT.BOLD;
    text.setStyleRange(style1);
    text.setEditable(false);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
}

View on GitHub

Snippet 6

StyledText  Snippet 6

Demonstrates StyledText with different fonts, colors, and styles applied to different portions of text.

This example shows how to apply:

  • Different font sizes (8pt, 12pt, 18pt, 24pt)

  • Different font families

  • Combined font styles (bold, italic)

  • Different foreground colors

  • Different background colors

StyledTextSnippet6.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Demonstrates StyledText with different fonts, colors, and styles applied to different portions of text.
 * This example shows how to apply:
 * - Different font sizes (8pt, 12pt, 18pt, 24pt)
 * - Different font families
 * - Combined font styles (bold, italic)
 * - Different foreground colors
 * - Different background colors
 */
public class StyledTextSnippet6 {

    public static void main(String[] args) throws ClassNotFoundException {
        Config.useEquo(StyledText.class);
        Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
        Config.useEquo(StyleRange.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("StyledText Snippet 6 - Fonts, Colors & Styles");
        shell.setLayout(new FillLayout());
        shell.setSize(700, 250);

        StyledText text = new StyledText(shell, SWT.BORDER | SWT.WRAP);
        text.setText("Red 8pt - Blue Italic 12pt - Green Bold 18pt - Purple Bold Italic 24pt - Yellow on Black");

        // Colors
        Color red = new Color(display, 255, 0, 0);
        Color blue = new Color(display, 0, 0, 255);
        Color green = new Color(display, 0, 128, 0);
        Color purple = new Color(display, 128, 0, 128);
        Color yellow = new Color(display, 255, 255, 0);
        Color black = new Color(display, 0, 0, 0);

        // Text: "Red 8pt - Blue Italic 12pt - Green Bold 18pt - Purple Bold Italic 24pt - Yellow on Black"
        // Indices: 0-6 "Red 8pt", 10-25 "Blue Italic 12pt", 29-43 "Green Bold 18pt", 47-69 "Purple Bold Italic 24pt", 73-87 "Yellow on Black"

        // Style 1: Small red font (8pt)
        StyleRange style1 = new StyleRange();
        style1.start = 0;
        style1.length = 7; // "Red 8pt"
        style1.font = new Font(display, "Arial", 8, SWT.NORMAL);
        style1.foreground = red;
        text.setStyleRange(style1);

        // Style 2: Blue italic font (12pt)
        StyleRange style2 = new StyleRange();
        style2.start = 10;
        style2.length = 16; // "Blue Italic 12pt"
        style2.font = new Font(display, "Arial", 12, SWT.ITALIC);
        style2.foreground = blue;
        text.setStyleRange(style2);

        // Style 3: Green bold font (18pt)
        StyleRange style3 = new StyleRange();
        style3.start = 29;
        style3.length = 15; // "Green Bold 18pt"
        style3.font = new Font(display, "Arial", 18, SWT.BOLD);
        style3.foreground = green;
        text.setStyleRange(style3);

        // Style 4: Purple bold italic font (24pt)
        StyleRange style4 = new StyleRange();
        style4.start = 47;
        style4.length = 23; // "Purple Bold Italic 24pt"
        style4.font = new Font(display, "Arial", 24, SWT.BOLD | SWT.ITALIC);
        style4.foreground = purple;
        text.setStyleRange(style4);

        // Style 5: Yellow text on black background
        StyleRange style5 = new StyleRange();
        style5.start = 73;
        style5.length = 15; // "Yellow on Black"
        style5.font = new Font(display, "Arial", 14, SWT.BOLD);
        style5.foreground = yellow;
        style5.background = black;
        text.setStyleRange(style5);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

View on GitHub

Snippet 7

StyledText  Snippet 7

Reproduces the SwtFontData serialization issue:

Sets a font directly on the StyledText widget (like Eclipse editor does),

which triggers serialization of a SwtFont/SwtFontData.

StyledTextSnippet7.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Reproduces the SwtFontData serialization issue:
 * Sets a font directly on the StyledText widget (like Eclipse editor does),
 * which triggers serialization of a SwtFont/SwtFontData.
 */
public class StyledTextSnippet7 {

    public static void main(String[] args) throws ClassNotFoundException {
        Config.useEquo(StyledText.class);
        Config.useEquo(Class.forName("org.eclipse.swt.custom.StyledTextRenderer"));
        Config.useEquo(StyleRange.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("StyledText Snippet 7 - Font on Widget");
        shell.setLayout(new FillLayout());
        shell.setSize(700, 250);

        StyledText text = new StyledText(shell, SWT.BORDER | SWT.WRAP);
        text.setText("This text has a font set directly on the StyledText widget.");

        // Set font directly on the widget (like Eclipse editor does)
        text.setFont(new Font(display, "Consolas", 14, SWT.NORMAL));

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

View on GitHub