Skip to content

Table Examples

Instances of this class implement a selectable user interface object that displays a list of images and strings and issues notification when selected.

Multiple Editors

Table  Multiple Editors

Example demonstrating TableMultipleEditors.

TableMultipleEditors.java
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TableMultipleEditors {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Table with multiple editors");
        shell.setLayout(new FillLayout());

        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        String[] titles = { "ID", "Name", "Type", "Active" };
        int[] widths = { 80, 150, 120, 80 };

        for (int i = 0; i < titles.length; i++) {
            TableColumn col = new TableColumn(table, SWT.NONE);
            col.setText(titles[i]);
            col.setWidth(widths[i]);
        }

        for (int i = 1; i <= 5; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[] {
                    String.valueOf(i),
                    "Item " + i,
                    "A",
                    i % 2 == 0 ? "Yes" : "No" // TODO
            });
        }

        for (TableItem item : table.getItems()) {

            TableEditor textEditor = new TableEditor(table);
            Text text = new Text(table, SWT.NONE);
            text.setText(item.getText(1));
            text.addModifyListener(e ->
                    item.setText(1, text.getText())
            );
            textEditor.grabHorizontal = true;
            textEditor.setEditor(text, item, 1);

            TableEditor comboEditor = new TableEditor(table);
            Combo combo = new Combo(table, SWT.READ_ONLY);
            combo.setItems(new String[] { "A", "B", "C" });
            combo.setText(item.getText(2));
            combo.addListener(SWT.Selection, e ->
                    item.setText(2, combo.getText())
            );
            comboEditor.grabHorizontal = true;
            comboEditor.setEditor(combo, item, 2);

            TableEditor checkEditor = new TableEditor(table);
            Button check = new Button(table, SWT.CHECK);
            check.setSelection("Yes".equals(item.getText(3)));
            check.addListener(SWT.Selection, e ->
                    item.setText(3, check.getSelection() ? "Yes" : "No")
            );
            checkEditor.horizontalAlignment = SWT.CENTER;
            checkEditor.setEditor(check, item, 3);
        }

        shell.setSize(500, 300);
        shell.open();

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

View on GitHub

Snippet

Table  Snippet

Example demonstrating TableSnippet.

TableSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableSnippet {

    public static void main(String[] args) {
        Config.useEquo(Table.class);
        Config.useEquo(TableItem.class);
        Config.useEquo(TableColumn.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setText("TableSnippet");
        shell.setSize(400, 250);

        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn nameColumn = new TableColumn(table, SWT.NONE);
        nameColumn.setText("Name");
        nameColumn.setWidth(150);

        TableColumn ageColumn = new TableColumn(table, SWT.NONE);
        ageColumn.setText("Age");
        ageColumn.setWidth(80);

        TableColumn cityColumn = new TableColumn(table, SWT.NONE);
        cityColumn.setText("City");
        cityColumn.setWidth(120);

        String[][] data = {
                {"John Doe", "25", "New York"},
                {"Jane Smith", "30", "Los Angeles"},
                {"Bob Johnson", "35", "Chicago"},
                {"Alice Brown", "28", "Houston"},
                {"Charlie Wilson", "42", "Phoenix"}
        };

        for (String[] row : data) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(row);
        }

        for (TableColumn column : table.getColumns()) {
            column.pack();
        }

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

View on GitHub

With Editors Example

Table  With Editors Example

Example demonstrating TableWithEditorsExample.

TableWithEditorsExample.java
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TableWithEditorsExample {

    public static void main(String[] args) {

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Table with Editors");
        shell.setSize(700, 400);
        shell.setLayout(new FillLayout());

        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        String[] titles = {"Name", "Type", "Value", "Enabled"};
        for (String title : titles) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(title);
            column.setWidth(170);
        }

        createItem(table, "Item 1", "Category", "N/A", true);
        createItem(table, "Item 2", "Text", "Editable", false);
        createItem(table, "Item 3", "Combo", "Option 1", true);

        TableEditor editor = new TableEditor(table);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;

//        table.addListener(SWT.MouseDoubleClick, event -> {
            table.addListener(SWT.MouseDown, event -> {

            int row = table.getSelectionIndex();
            if (row == -1) return;

            TableItem item = table.getItem(row);

            // Detectar columna clickeada
            int column = -1;
            for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(event.x, event.y)) {
                    column = i;
                    break;
                }
            }

            if (column == -1) return;

            Control oldEditor = editor.getEditor();
            if (oldEditor != null) oldEditor.dispose();

            // Name y Value → Text
            if (column == 0 || column == 2) {

                Text text = new Text(table, SWT.NONE);
                text.setText(item.getText(column));
                text.selectAll();
                text.setFocus();

                int col = column;

                text.addListener(SWT.FocusOut, e -> {
                    item.setText(col, text.getText());
                    text.dispose();
                });

                text.addListener(SWT.Traverse, e -> {
                    if (e.detail == SWT.TRAVERSE_RETURN) {
                        item.setText(col, text.getText());
                        text.dispose();
                        e.doit = false;
                    }
                    if (e.detail == SWT.TRAVERSE_ESCAPE) {
                        text.dispose();
                        e.doit = false;
                    }
                });

                editor.setEditor(text, item, column);
            }

            // Type → Combo
            else if (column == 1) {

                Combo combo = new Combo(table, SWT.READ_ONLY);
                combo.setItems("Text", "Combo", "Category");
                combo.setText(item.getText(column));
                combo.setFocus();

                int col = column;

                combo.addListener(SWT.Selection, e -> {
                    item.setText(col, combo.getText());
                    combo.dispose();
                });

                combo.addListener(SWT.FocusOut, e -> combo.dispose());

                editor.setEditor(combo, item, column);
            }

            // Enabled → Checkbox
            else if (column == 3) {

                Button check = new Button(table, SWT.CHECK);

                Boolean enabled = (Boolean) item.getData("enabled");
                check.setSelection(enabled != null && enabled);
                check.setFocus();

                int col = column;

                check.addListener(SWT.Selection, e -> {
                    boolean value = check.getSelection();
                    item.setData("enabled", value);
                    item.setText(col, value ? "Yes" : "No");
                });

                check.addListener(SWT.FocusOut, e -> check.dispose());

                editor.grabHorizontal = false;
                editor.minimumWidth = 50;
                editor.setEditor(check, item, column);
            }
        });

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

    private static void createItem(Table table, String name, String type, String value, boolean enabled) {

        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[]{
                name,
                type,
                value,
                enabled ? "Yes" : "No"
        });

        item.setData("enabled", enabled);
    }
}

View on GitHub

With Text And Checkbox

Table  With Text And Checkbox

Example demonstrating TableWithTextAndCheckbox.

TableWithTextAndCheckbox.java
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.TableEditor;

public class TableWithTextAndCheckbox {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Table with Text and Checkbox");
        shell.setSize(400, 300);
        shell.setLayout(new FillLayout());

        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn colText = new TableColumn(table, SWT.NONE);
        colText.setText("Text");

        TableColumn colCheck = new TableColumn(table, SWT.NONE);
        colCheck.setText("Active");

        for (int i = 0; i < 5; i++) {
            TableItem item = new TableItem(table, SWT.NONE);

            TableEditor textEditor = new TableEditor(table);
            Text text = new Text(table, SWT.NONE);
            text.setText("Row " + (i + 1));
            textEditor.grabHorizontal = true;
            textEditor.setEditor(text, item, 0);

            TableEditor checkEditor = new TableEditor(table);
            Button checkbox = new Button(table, SWT.CHECK);
            checkbox.setSelection(false);
            checkEditor.horizontalAlignment = SWT.CENTER;
            checkEditor.setEditor(checkbox, item, 1);
        }

        colText.pack();
        colCheck.pack();

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

View on GitHub