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 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) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText Snippet 1");
    shell.setLayout(new FillLayout());
    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.pack();
    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 org.eclipse.swt.SWT;
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) {
        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 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) {
        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);

        // Primer párrafo: negrita
        StyleRange style1 = new StyleRange();
        style1.start = 0;
        style1.length = 78;
        style1.fontStyle = SWT.BOLD;
        styledText.setStyleRange(style1);

        // Segundo párrafo: color rojo
        StyleRange style2 = new StyleRange();
        style2.start = 80;
        style2.length = 62;
        style2.foreground = display.getSystemColor(SWT.COLOR_RED);
        styledText.setStyleRange(style2);

        // Tercer párrafo: fondo azul
        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 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) {
    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 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) {
    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