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 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) {
        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

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 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) {
        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 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) {
        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

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 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) {
        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 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) {
        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