Skip to content

Text Examples

Instances of this class are selectable user interface objects that allow the user to enter and modify text.

Alignment Snippet

Text  Alignment Snippet

Demonstrates Text alignment

This example showcases:

  • Left-aligned text (default)

  • Center-aligned text (SWT.CENTER)

  • Right-aligned text (SWT.RIGHT)

TextAlignmentSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Text alignment
 * This example showcases:
 * - Left-aligned text (default)
 * - Center-aligned text (SWT.CENTER)
 * - Right-aligned text (SWT.RIGHT)
 */
public class TextAlignmentSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TextAlignmentSnippet");
        shell.setLayout(new GridLayout(2, false));

        // Left-aligned (default)
        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("Left:");
        Text leftText = new Text(shell, SWT.BORDER);
        leftText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        leftText.setText("Left aligned");

        // Center-aligned
        Label label2 = new Label(shell, SWT.NONE);
        label2.setText("Center:");
        Text centerText = new Text(shell, SWT.BORDER | SWT.CENTER);
        centerText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        centerText.setText("Center aligned");

        // Right-aligned
        Label label3 = new Label(shell, SWT.NONE);
        label3.setText("Right:");
        Text rightText = new Text(shell, SWT.BORDER | SWT.RIGHT);
        rightText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        rightText.setText("Right aligned");

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

View on GitHub

Arrow Row Snippet

Text  Arrow Row Snippet

A row of single-line controls in a RowLayout, each top-aligned at its preferred height: two fields

made of a borderless Text with an ARROW button beside it, two check boxes and a read-only Combo.

The field is as tall as the taller of its Text and its arrow.

TextArrowRowSnippet.java
package dev.equo.internal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

/**
 * A row of single-line controls in a RowLayout, each top-aligned at its preferred height: two fields
 * made of a borderless Text with an ARROW button beside it, two check boxes and a read-only Combo.
 * The field is as tall as the taller of its Text and its arrow.
 */
public class TextArrowRowSnippet {

    static class ArrowField extends Composite {
        final Text text;
        final Button arrow;

        ArrowField(Composite parent, int style) {
            super(parent, style);
            text = new Text(this, SWT.SINGLE);
            text.setText("2026-01-01");
            arrow = new Button(this, SWT.ARROW | SWT.DOWN);
            addListener(SWT.Resize, e -> {
                Rectangle area = getClientArea();
                Point arrowSize = arrow.computeSize(SWT.DEFAULT, area.height);
                text.setBounds(0, 0, area.width - arrowSize.x, area.height);
                arrow.setBounds(area.width - arrowSize.x, 0, arrowSize.x, arrowSize.y);
            });
        }

        @Override
        public Point computeSize(int wHint, int hHint, boolean changed) {
            Point textSize = text.computeSize(wHint, SWT.DEFAULT, changed);
            Point arrowSize = arrow.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
            int border = getBorderWidth();
            int height = Math.max(hHint, Math.max(textSize.y, arrowSize.y) + 2 * border);
            int width = Math.max(wHint, textSize.x + arrowSize.x + 2 * border);
            return new Point(width, height);
        }
    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Text and ARROW row");
        shell.setLayout(new FillLayout(SWT.VERTICAL));

        Composite row = new Composite(shell, SWT.BORDER);
        row.setLayout(new RowLayout());
        ArrowField from = new ArrowField(row, SWT.BORDER);
        ArrowField to = new ArrowField(row, SWT.BORDER);
        Button first = new Button(row, SWT.CHECK);
        first.setText("first");
        Button second = new Button(row, SWT.CHECK);
        second.setText("second");
        Combo choice = new Combo(row, SWT.READ_ONLY);
        choice.setItems("All", "A", "B");
        choice.select(0);

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

View on GitHub

Limit Snippet

Text  Limit Snippet

Demonstrates Text character limit

This example showcases:

  • Text field with character limit using setTextLimit()

  • Visual feedback of remaining characters

TextLimitSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Text character limit
 * This example showcases:
 * - Text field with character limit using setTextLimit()
 * - Visual feedback of remaining characters
 */
public class TextLimitSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TextLimitSnippet");
        shell.setLayout(new GridLayout(1, false));

        // Text with 10 character limit
        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("Username (max 10 characters):");

        Text limitedText = new Text(shell, SWT.BORDER);
        limitedText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        limitedText.setTextLimit(10);
        limitedText.setMessage("Enter username");
        limitedText.addListener(SWT.Modify, e -> {
            int length = limitedText.getText().length();
            int remaining = 10 - length;
            System.out.println("Characters used: " + length + "/10 (remaining: " + remaining + ")");
        });

        // Add some spacing
        Label spacer = new Label(shell, SWT.NONE);
        spacer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // Text with 50 character limit
        Label label2 = new Label(shell, SWT.NONE);
        label2.setText("Description (max 50 characters):");

        Text limitedText2 = new Text(shell, SWT.BORDER);
        limitedText2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        limitedText2.setTextLimit(50);
        limitedText2.setMessage("Enter description");

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

View on GitHub

Multi Line Snippet

Text  Multi Line Snippet

Demonstrates Multi-line Text widget

This example showcases:

  • Multi-line text area with SWT.MULTI

  • Vertical scrolling

  • Text modification events

TextMultiLineSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Multi-line Text widget
 * This example showcases:
 * - Multi-line text area with SWT.MULTI
 * - Vertical scrolling
 * - Text modification events
 */
public class TextMultiLineSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TextMultiLineSnippet");
        shell.setLayout(new GridLayout(1, false));

        // Multi-line text area
        Label label = new Label(shell, SWT.NONE);
        label.setText("Multi-line text area:");

        Text multiText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        multiText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        multiText.setMessage("Enter multiple lines of text...");
        multiText.setText("Line 1\nLine 2\nLine 3");
        multiText.addListener(SWT.Modify, e -> System.out.println("Text modified: " + multiText.getText()));

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

View on GitHub

Password Snippet

Text  Password Snippet

Demonstrates Password Text widget

This example showcases:

  • Password field with SWT.PASSWORD style

  • Text modification events

  • Enter key handling

TextPasswordSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Password Text widget
 * This example showcases:
 * - Password field with SWT.PASSWORD style
 * - Text modification events
 * - Enter key handling
 */
public class TextPasswordSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);
        Config.useEquo(Label.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("PasswordSnippet");
        shell.setLayout(new GridLayout(2, false));

        // Password field
        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("Password:");
        Text passwordText = new Text(shell, SWT.BORDER | SWT.PASSWORD);
        passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        passwordText.setMessage("Enter password...");
        passwordText.addListener(SWT.Modify, e -> System.out.println("Password modified (length: " + passwordText.getText().length() + ")"));
        passwordText.addListener(SWT.DefaultSelection, e -> System.out.println("Enter pressed, password: " + passwordText.getText()));

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

View on GitHub

Search Snippet

Text  Search Snippet

Demonstrates Search Text widget

This example showcases:

  • Search field with SWT.SEARCH style

  • Search icon prefix

  • Cancel/clear button suffix

  • DefaultSelection event (when Enter is pressed)

TextSearchSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Search Text widget
 * This example showcases:
 * - Search field with SWT.SEARCH style
 * - Search icon prefix
 * - Cancel/clear button suffix
 * - DefaultSelection event (when Enter is pressed)
 */
public class TextSearchSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TextSearchSnippet");
        shell.setLayout(new GridLayout(1, false));

        // Search field with icon
        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("Search field:");

        Text searchText = new Text(shell, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
        searchText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        searchText.setMessage("Search...");
        searchText.addListener(SWT.Modify, e -> System.out.println("Search text changed: " + searchText.getText()));
        searchText.addListener(SWT.DefaultSelection, e -> {
            Event event = (Event) e;
            if (event.detail == SWT.ICON_CANCEL) {
                System.out.println("Cancel button clicked");
            } else {
                System.out.println("Search submitted: " + searchText.getText());
            }
        });

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

View on GitHub

Snippet

Text  Snippet

Demonstrates Text widget

This example showcases:

  • Single-line editable text field

  • Password field

TextSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates Text widget
 * This example showcases:
 * - Single-line editable text field
 * - Password field
 */
public class TextSnippet {
    public static void main(String[] args) {
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TextSnippet");
        shell.setLayout(new GridLayout(2, false));

        // Single-line text field
        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("Single-line:");
        Text text1 = new Text(shell, SWT.BORDER);
        text1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        text1.setMessage("Enter text here...");
        text1.addListener(SWT.Modify, e -> System.out.println("Text modified: " + text1.getText()));
        text1.addListener(SWT.DefaultSelection, e -> System.out.println("Enter pressed: " + text1.getText()));

        // Read-only text field
        Label label2 = new Label(shell, SWT.NONE);
        label2.setText("Read-only:");
        Text text2 = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
        text2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        text2.setText("This text is read-only");

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

View on GitHub