Skip to content

CCombo Examples

The CCombo class represents a selectable user interface object that combines a text field and a list and issues notification when an item is selected from the list.

Alignment Snippet

CCombo  Alignment Snippet

CCombo with different text alignments

CComboAlignmentSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * CCombo with different text alignments
 */
public class CComboAlignmentSnippet {
    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Config.useEquo(Group.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo - Text Alignment");
        shell.setLayout(new GridLayout(1, false));

        String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};

        // Alignment group
        Group alignmentGroup = new Group(shell, SWT.NONE);
        alignmentGroup.setText("Text Alignment");
        alignmentGroup.setLayout(new GridLayout(2, false));
        alignmentGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label1 = new Label(alignmentGroup, SWT.NONE);
        label1.setText("LEFT aligned:");

        CCombo leftCombo = new CCombo(alignmentGroup, SWT.BORDER | SWT.READ_ONLY | SWT.LEFT);
        leftCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        leftCombo.setItems(items);
        leftCombo.select(0);

        Label label2 = new Label(alignmentGroup, SWT.NONE);
        label2.setText("CENTER aligned:");

        CCombo centerCombo = new CCombo(alignmentGroup, SWT.BORDER | SWT.READ_ONLY | SWT.CENTER);
        centerCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        centerCombo.setItems(items);
        centerCombo.select(1);

        Label label3 = new Label(alignmentGroup, SWT.NONE);
        label3.setText("RIGHT aligned:");

        CCombo rightCombo = new CCombo(alignmentGroup, SWT.BORDER | SWT.READ_ONLY | SWT.RIGHT);
        rightCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        rightCombo.setItems(items);
        rightCombo.select(2);

        // Selection info group
        Group infoGroup = new Group(shell, SWT.NONE);
        infoGroup.setText("Selection Info");
        infoGroup.setLayout(new GridLayout(2, false));
        infoGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label infoLabel = new Label(infoGroup, SWT.NONE);
        infoLabel.setText("Selected:");

        Label selectionValue = new Label(infoGroup, SWT.BORDER);
        selectionValue.setLayoutData(new GridData(200, SWT.DEFAULT));
        selectionValue.setText("Apple");

        // Add listeners
        SelectionAdapter updateSelection = new SelectionAdapter() {
            @Override
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                CCombo source = (CCombo) e.widget;
                selectionValue.setText(source.getText());
                System.out.println("Selected: " + source.getText());
            }
        };

        leftCombo.addSelectionListener(updateSelection);
        centerCombo.addSelectionListener(updateSelection);
        rightCombo.addSelectionListener(updateSelection);

        shell.pack();
        shell.open();

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

View on GitHub

Border Snippet

CCombo  Border Snippet

CCombo with different border styles

CComboBorderSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * CCombo with different border styles
 */
public class CComboBorderSnippet {
    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Config.useEquo(Group.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo - Border Styles");
        shell.setLayout(new GridLayout(1, false));

        String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};

        // Border styles group
        Group borderGroup = new Group(shell, SWT.NONE);
        borderGroup.setText("Border Styles");
        borderGroup.setLayout(new GridLayout(2, false));
        borderGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label1 = new Label(borderGroup, SWT.NONE);
        label1.setText("BORDER:");

        CCombo borderedCombo = new CCombo(borderGroup, SWT.BORDER | SWT.READ_ONLY);
        borderedCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        borderedCombo.setItems(items);
        borderedCombo.select(0);

        Label label2 = new Label(borderGroup, SWT.NONE);
        label2.setText("FLAT:");

        CCombo flatCombo = new CCombo(borderGroup, SWT.FLAT | SWT.READ_ONLY);
        flatCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        flatCombo.setItems(items);
        flatCombo.select(1);

        Label label3 = new Label(borderGroup, SWT.NONE);
        label3.setText("No style:");

        CCombo plainCombo = new CCombo(borderGroup, SWT.READ_ONLY);
        plainCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        plainCombo.setItems(items);
        plainCombo.select(2);

        // Selection info group
        Group infoGroup = new Group(shell, SWT.NONE);
        infoGroup.setText("Selection Info");
        infoGroup.setLayout(new GridLayout(2, false));
        infoGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label infoLabel = new Label(infoGroup, SWT.NONE);
        infoLabel.setText("Selected:");

        Label selectionValue = new Label(infoGroup, SWT.BORDER);
        selectionValue.setLayoutData(new GridData(200, SWT.DEFAULT));
        selectionValue.setText("Apple");

        // Add listeners
        SelectionAdapter updateSelection = new SelectionAdapter() {
            @Override
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                CCombo source = (CCombo) e.widget;
                selectionValue.setText(source.getText());
                System.out.println("Selected: " + source.getText());
            }
        };

        borderedCombo.addSelectionListener(updateSelection);
        flatCombo.addSelectionListener(updateSelection);
        plainCombo.addSelectionListener(updateSelection);

        shell.pack();
        shell.open();

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

View on GitHub

Colors Snippet

CCombo  Colors Snippet

CCombo with custom colors

CComboColorsSnippet.java
package dev.equo;

import static org.eclipse.swt.events.SelectionListener.*;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * CCombo with custom colors
 */
public class CComboColorsSnippet {
    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Config.useEquo(Group.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo - Custom Colors");
        shell.setLayout(new GridLayout(1, false));

        String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};

        // Custom colors group
        Group colorGroup = new Group(shell, SWT.NONE);
        colorGroup.setText("Custom Colors");
        colorGroup.setLayout(new GridLayout(2, false));
        colorGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label1 = new Label(colorGroup, SWT.NONE);
        label1.setText("Default colors:");

        CCombo defaultCombo = new CCombo(colorGroup, SWT.BORDER | SWT.READ_ONLY);
        defaultCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        defaultCombo.setItems(items);
        defaultCombo.select(0);

        Label label2 = new Label(colorGroup, SWT.NONE);
        label2.setText("Blue background:");

        CCombo blueCombo = new CCombo(colorGroup, SWT.BORDER | SWT.READ_ONLY);
        blueCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        blueCombo.setItems(items);
        blueCombo.select(1);
        blueCombo.setBackground(new Color(220, 240, 255)); // Light blue

        Label label3 = new Label(colorGroup, SWT.NONE);
        label3.setText("Red foreground:");

        CCombo redTextCombo = new CCombo(colorGroup, SWT.BORDER | SWT.READ_ONLY | SWT.CENTER);
        redTextCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        redTextCombo.setItems(items);
        redTextCombo.select(2);
        redTextCombo.setForeground(new Color(200, 0, 0)); // Dark red

        Label label4 = new Label(colorGroup, SWT.NONE);
        label4.setText("Green bg + white fg:");

        CCombo greenCombo = new CCombo(colorGroup, SWT.BORDER | SWT.READ_ONLY | SWT.RIGHT);
        greenCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        greenCombo.setItems(items);
        greenCombo.select(3);
        greenCombo.setBackground(new Color(0, 120, 0)); // Dark green
        greenCombo.setForeground(new Color(255, 255, 255)); // White

        // Selection info group
        Group infoGroup = new Group(shell, SWT.NONE);
        infoGroup.setText("Selection Info");
        infoGroup.setLayout(new GridLayout(2, false));
        infoGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label infoLabel = new Label(infoGroup, SWT.NONE);
        infoLabel.setText("Selected:");

        Label selectionValue = new Label(infoGroup, SWT.BORDER);
        selectionValue.setLayoutData(new GridData(200, SWT.DEFAULT));
        selectionValue.setText("Apple");

        // Add listeners
        SelectionAdapter updateSelection = new SelectionAdapter() {
            @Override
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                CCombo source = (CCombo) e.widget;
                selectionValue.setText(source.getText());
                System.out.println("Selected: " + source.getText());
            }
        };

        defaultCombo.addSelectionListener(updateSelection);
        blueCombo.addSelectionListener(updateSelection);
        redTextCombo.addSelectionListener(updateSelection);
        greenCombo.addSelectionListener(updateSelection);

        shell.pack();
        shell.open();

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

View on GitHub

Dark Theme Snippet

CCombo  Dark Theme Snippet

CCombo under a dark theme, with and without application-supplied colors.

A themed application colors its own controls, handing each one a color it read from

Display#getSystemColor. Every row is the same CCombo; only where its background and

foreground come from differs. Open each dropdown and compare the closed field, the

dropdown panel and the item text against each other and against the dark surface.

CComboDarkThemeSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * CCombo under a dark theme, with and without application-supplied colors.
 *
 * A themed application colors its own controls, handing each one a color it read from
 * Display#getSystemColor. Every row is the same CCombo; only where its background and
 * foreground come from differs. Open each dropdown and compare the closed field, the
 * dropdown panel and the item text against each other and against the dark surface.
 */
public class CComboDarkThemeSnippet {

    private static final String[] ITEMS = { "TWT", "TVD", "TVDSS", "FREQ" };

    public static void main(String[] args) {
        System.setProperty("swt.evolve.force_theme", "dark");

        Config.useEquo(CCombo.class);
        Config.useEquo(Label.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo - dark theme color sources");
        shell.setLayout(new GridLayout(2, false));

        row(shell, "theme only (no color set)", null, null);
        row(shell, "WIDGET_BACKGROUND / WIDGET_FOREGROUND",
                display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND),
                display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
        row(shell, "LIST_BACKGROUND / LIST_FOREGROUND",
                display.getSystemColor(SWT.COLOR_LIST_BACKGROUND),
                display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        row(shell, "TEXT_DISABLED_BACKGROUND / LIST_FOREGROUND",
                display.getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND),
                display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));

        shell.setSize(560, 260);
        shell.open();

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

    private static void row(Composite parent, String name, Color background, Color foreground) {
        Label label = new Label(parent, SWT.NONE);
        label.setText(name);

        CCombo combo = new CCombo(parent, SWT.BORDER | SWT.READ_ONLY);
        combo.setLayoutData(new GridData(180, SWT.DEFAULT));
        combo.setItems(ITEMS);
        combo.select(2);
        if (background != null)
            combo.setBackground(background);
        if (foreground != null)
            combo.setForeground(foreground);

        System.out.println(name
                + " -> background=" + rgb(background)
                + ", foreground=" + rgb(foreground));
    }

    private static String rgb(Color color) {
        return color == null
                ? "unset"
                : String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue());
    }
}

View on GitHub

Editable Snippet

CCombo  Editable Snippet

CCombo Editable - Shows editable CCombo where user can type custom text

Key difference: Regular Combo READ_ONLY can't be made editable on all platforms

CCombo allows switching between editable and read-only at runtime

CComboEditableSnippet.java
package dev.equo;

import static org.eclipse.swt.events.SelectionListener.*;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * CCombo Editable - Shows editable CCombo where user can type custom text
 * Key difference: Regular Combo READ_ONLY can't be made editable on all platforms
 * CCombo allows switching between editable and read-only at runtime
 */
public class CComboEditableSnippet {
    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Config.useEquo(Group.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo Editable Example");
        shell.setLayout(new GridLayout(1, false));

        // Editable CCombo - user can type anything
        Group editableGroup = new Group(shell, SWT.NONE);
        editableGroup.setText("Editable CCombo");
        editableGroup.setLayout(new GridLayout(2, false));
        editableGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label1 = new Label(editableGroup, SWT.NONE);
        label1.setText("Type or select:");

        CCombo editableCombo = new CCombo(editableGroup, SWT.BORDER);
        editableCombo.setLayoutData(new GridData(200, SWT.DEFAULT));
        editableCombo.setItems(new String[] {"Red", "Green", "Blue", "Yellow", "Orange"});
        editableCombo.setText("Type custom text...");

        // User can type custom text not in the list
        editableCombo.addModifyListener(e -> {
            System.out.println("Text changed to: " + editableCombo.getText());
        });

        // Toggle editable state
        Group toggleGroup = new Group(shell, SWT.NONE);
        toggleGroup.setText("Toggle Editable State");
        toggleGroup.setLayout(new GridLayout(2, false));
        toggleGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label2 = new Label(toggleGroup, SWT.NONE);
        label2.setText("Current state:");

        Button toggleButton = new Button(toggleGroup, SWT.PUSH);
        toggleButton.setText("Make Read-Only");
        toggleButton.addSelectionListener(widgetSelectedAdapter(e -> {
            boolean isEditable = editableCombo.getEditable();
            editableCombo.setEditable(!isEditable);
            toggleButton.setText(isEditable ? "Make Editable" : "Make Read-Only");
            System.out.println("CCombo is now " + (isEditable ? "READ-ONLY" : "EDITABLE"));
        }));

        // Show current text
        Group displayGroup = new Group(shell, SWT.NONE);
        displayGroup.setText("Current Text Display");
        displayGroup.setLayout(new GridLayout(2, false));
        displayGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label label3 = new Label(displayGroup, SWT.NONE);
        label3.setText("Current text:");

        Label currentText = new Label(displayGroup, SWT.BORDER);
        currentText.setLayoutData(new GridData(200, SWT.DEFAULT));
        currentText.setText("");

        editableCombo.addSelectionListener(widgetSelectedAdapter(e -> {
            currentText.setText(editableCombo.getText());
        }));

        shell.pack();
        shell.open();

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

View on GitHub

Keyboard Snippet

CCombo  Keyboard Snippet

Drives a READ_ONLY and an editable CCombo from the keyboard, reached with Tab. Alt+Down opens the

list; moving with Down and pressing Enter puts the item into the text, fires Selection and closes

the list; Enter on the closed CCombo fires DefaultSelection. After every step the selection index,

text and list visibility are printed. Every event is printed with a [repro] prefix. Run with

{@code --drive} to send the keys with java.awt.Robot. For the native baseline, call

Config.defaultToEclipse() in place of the useEquo lines and run it with runEmbedExample.

CComboKeyboardSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;

/**
 * Drives a READ_ONLY and an editable CCombo from the keyboard, reached with Tab. Alt+Down opens the
 * list; moving with Down and pressing Enter puts the item into the text, fires Selection and closes
 * the list; Enter on the closed CCombo fires DefaultSelection. After every step the selection index,
 * text and list visibility are printed. Every event is printed with a [repro] prefix. Run with
 * {@code --drive} to send the keys with java.awt.Robot. For the native baseline, call
 * Config.defaultToEclipse() in place of the useEquo lines and run it with runEmbedExample.
 */
public class CComboKeyboardSnippet {
    static final String[] ITEMS = {"Alpha", "Bravo", "Charlie", "Delta", "Echo"};

    public static void main(String[] args) throws Exception {
        Config.useEquo(CCombo.class);
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CComboKeyboardSnippet");
        shell.setLayout(new RowLayout(SWT.VERTICAL));

        Text start = new Text(shell, SWT.BORDER);
        start.setText("start");
        CCombo readOnly = combo(shell, SWT.READ_ONLY | SWT.BORDER, "READ_ONLY");
        CCombo editable = combo(shell, SWT.BORDER, "editable");

        shell.setSize(300, 220);
        shell.open();
        start.setFocus();
        if (Arrays.asList(args).contains("--drive")) drive(display, shell, start, readOnly, editable);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
        System.exit(0);
    }

    static CCombo combo(Shell shell, int style, String name) {
        CCombo combo = new CCombo(shell, style);
        combo.setItems(ITEMS);
        combo.select(0);
        combo.addListener(SWT.Selection, e -> log(name + " Selection index=" + combo.getSelectionIndex() + " text=" + combo.getText()));
        combo.addListener(SWT.DefaultSelection, e -> log(name + " DefaultSelection"));
        combo.addListener(SWT.FocusIn, e -> log(name + " FocusIn"));
        return combo;
    }

    static void drive(Display display, Shell shell, Control first, CCombo readOnly, CCombo editable) {
        Thread t = new Thread(() -> {
            try {
                Robot r = new Robot();
                r.delay(6000);
                activate(r, display, shell, first);
                display.syncExec(() -> log("focus control: " + display.getFocusControl()));
                for (CCombo combo : new CCombo[] {readOnly, editable}) {
                    step(r, display, combo, "Tab", KeyEvent.VK_TAB);
                    runKeys(r, display, combo);
                }
                log("=== focus by click");
                for (CCombo combo : new CCombo[] {readOnly, editable}) {
                    display.syncExec(() -> combo.select(0));
                    click(r, display, combo);
                    display.syncExec(() -> log("  after click: listVisible=" + combo.getListVisible() + " focus=" + display.getFocusControl()));
                    if (combo == readOnly) {
                        step(r, display, combo, "Escape (close the list the click opened)", KeyEvent.VK_ESCAPE);
                    }
                    runKeys(r, display, combo);
                }
                log("done");
                display.asyncExec(shell::dispose);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        t.setDaemon(true);
        t.start();
    }

    static void runKeys(Robot r, Display display, CCombo combo) {
        step(r, display, combo, "Alt+Down", KeyEvent.VK_ALT, KeyEvent.VK_DOWN);
        step(r, display, combo, "Down", KeyEvent.VK_DOWN);
        step(r, display, combo, "Down", KeyEvent.VK_DOWN);
        step(r, display, combo, "Enter (in the list)", KeyEvent.VK_ENTER);
        step(r, display, combo, "Enter (list closed)", KeyEvent.VK_ENTER);
    }

    static void step(Robot r, Display display, CCombo combo, String label, int... keys) {
        log("--- " + label);
        for (int k : keys) r.keyPress(k);
        for (int i = keys.length - 1; i >= 0; i--) r.keyRelease(keys[i]);
        r.delay(1200);
        display.syncExec(() -> log("  state index=" + combo.getSelectionIndex() + " text=" + combo.getText()
                + " listVisible=" + combo.getListVisible() + " focus=" + display.getFocusControl()));
    }

    static void activate(Robot r, Display display, Shell shell, Control control) {
        display.syncExec(shell::forceActive);
        r.delay(500);
        Point[] center = new Point[1];
        display.syncExec(() -> {
            if (display.getFocusControl() == null) {
                Point size = control.getSize();
                center[0] = display.map(control, null, size.x / 2, size.y / 2);
            }
        });
        if (center[0] != null) {
            r.mouseMove(center[0].x, center[0].y);
            r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            r.delay(800);
        }
    }

    static void click(Robot r, Display display, Control control) {
        Point[] center = new Point[1];
        display.syncExec(() -> {
            Point size = control.getSize();
            center[0] = display.map(control, null, size.x / 2, size.y / 2);
        });
        r.mouseMove(center[0].x, center[0].y);
        r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        r.delay(1200);
    }

    static void log(String s) {
        System.out.println("[repro] " + s);
    }
}

View on GitHub

Modify Snippet

CCombo  Modify Snippet

When a CCombo fires Modify, getText() already returns the new text. The listener prints getText()

for each Modify while the selection is changed from the keyboard and while text is typed. Every

event is printed with a [repro] prefix. Run with {@code --drive} to send the keys with

java.awt.Robot. For the native baseline, call

Config.defaultToEclipse() in place of the useEquo lines and run it with runEmbedExample.

CComboModifySnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;

/**
 * When a CCombo fires Modify, getText() already returns the new text. The listener prints getText()
 * for each Modify while the selection is changed from the keyboard and while text is typed. Every
 * event is printed with a [repro] prefix. Run with {@code --drive} to send the keys with
 * java.awt.Robot. For the native baseline, call
 * Config.defaultToEclipse() in place of the useEquo lines and run it with runEmbedExample.
 */
public class CComboModifySnippet {
    static final String[] ITEMS = {"Alpha", "Bravo", "Charlie", "Delta", "Echo"};

    public static void main(String[] args) throws Exception {
        Config.useEquo(CCombo.class);
        Config.useEquo(Text.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CComboModifySnippet");
        shell.setLayout(new RowLayout(SWT.VERTICAL));

        Text start = new Text(shell, SWT.BORDER);
        start.setText("start");
        CCombo readOnly = combo(shell, SWT.READ_ONLY | SWT.BORDER, "READ_ONLY");
        CCombo editable = combo(shell, SWT.BORDER, "editable");

        shell.setSize(300, 220);
        shell.open();
        start.setFocus();
        if (Arrays.asList(args).contains("--drive")) drive(display, shell, start, readOnly, editable);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
        System.exit(0);
    }

    static CCombo combo(Shell shell, int style, String name) {
        CCombo combo = new CCombo(shell, style);
        combo.setItems(ITEMS);
        combo.select(0);
        combo.addListener(SWT.Modify, e -> log(name + " Modify getText()=" + combo.getText()));
        combo.addListener(SWT.Selection, e -> log(name + " Selection index=" + combo.getSelectionIndex()));
        return combo;
    }

    static void drive(Display display, Shell shell, Control first, CCombo readOnly, CCombo editable) {
        Thread t = new Thread(() -> {
            try {
                Robot r = new Robot();
                r.delay(6000);
                activate(r, display, shell, first);
                display.syncExec(() -> log("focus control: " + display.getFocusControl()));
                step(r, display, readOnly, "Tab", KeyEvent.VK_TAB);
                step(r, display, readOnly, "Down", KeyEvent.VK_DOWN);
                step(r, display, readOnly, "Down", KeyEvent.VK_DOWN);
                step(r, display, readOnly, "End", KeyEvent.VK_END);
                step(r, display, editable, "Tab", KeyEvent.VK_TAB);
                step(r, display, editable, "Down", KeyEvent.VK_DOWN);
                step(r, display, editable, "End", KeyEvent.VK_END);
                step(r, display, editable, "z", KeyEvent.VK_Z);
                step(r, display, editable, "y", KeyEvent.VK_Y);
                log("=== focus by click");
                display.syncExec(() -> { readOnly.select(0); editable.select(0); });
                click(r, display, readOnly);
                step(r, display, readOnly, "Escape (close the list the click opened)", KeyEvent.VK_ESCAPE);
                step(r, display, readOnly, "Down", KeyEvent.VK_DOWN);
                step(r, display, readOnly, "Down", KeyEvent.VK_DOWN);
                click(r, display, editable);
                step(r, display, editable, "Down", KeyEvent.VK_DOWN);
                step(r, display, editable, "End", KeyEvent.VK_END);
                step(r, display, editable, "z", KeyEvent.VK_Z);
                step(r, display, editable, "y", KeyEvent.VK_Y);
                log("done");
                display.asyncExec(shell::dispose);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        t.setDaemon(true);
        t.start();
    }

    static void step(Robot r, Display display, CCombo combo, String label, int... keys) {
        log("--- " + label);
        for (int k : keys) r.keyPress(k);
        for (int i = keys.length - 1; i >= 0; i--) r.keyRelease(keys[i]);
        r.delay(1200);
        display.syncExec(() -> log("  state text=" + combo.getText() + " focus=" + display.getFocusControl()));
    }

    static void activate(Robot r, Display display, Shell shell, Control control) {
        display.syncExec(shell::forceActive);
        r.delay(500);
        Point[] center = new Point[1];
        display.syncExec(() -> {
            if (display.getFocusControl() == null) {
                Point size = control.getSize();
                center[0] = display.map(control, null, size.x / 2, size.y / 2);
            }
        });
        if (center[0] != null) {
            r.mouseMove(center[0].x, center[0].y);
            r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            r.delay(800);
        }
    }

    static void click(Robot r, Display display, Control control) {
        Point[] center = new Point[1];
        display.syncExec(() -> {
            Point size = control.getSize();
            center[0] = display.map(control, null, size.x / 2, size.y / 2);
        });
        r.mouseMove(center[0].x, center[0].y);
        r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        r.delay(1200);
    }

    static void log(String s) {
        System.out.println("[repro] " + s);
    }
}

View on GitHub

Pinned Width Snippet

CCombo  Pinned Width Snippet

CCombo values at widths the application pins, the way a toolbar contribution does.

Each row pins a width through RowData and selects the value that has to survive it. The

value must be readable in full at every pinned width, exactly as a native CCombo shows it.

The last row pins nothing, so the combo takes its own preferred width.

Toggle Config.useEclipse(CCombo.class) below to compare against native SWT.

CComboPinnedWidthSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * CCombo values at widths the application pins, the way a toolbar contribution does.
 *
 * Each row pins a width through RowData and selects the value that has to survive it. The
 * value must be readable in full at every pinned width, exactly as a native CCombo shows it.
 * The last row pins nothing, so the combo takes its own preferred width.
 *
 * Toggle Config.useEclipse(CCombo.class) below to compare against native SWT.
 */
public class CComboPinnedWidthSnippet {

    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo - application-pinned widths");
        shell.setLayout(new GridLayout(2, false));

        String[] sizes = {"Small", "Medium", "Large", "X-Large"};
        String[] materials = {"Aluminium", "Steel", "Stainless Steel"};

        row(shell, "Pinned to 80px:", sizes, 3, 80);
        row(shell, "Pinned to 145px:", materials, 2, 145);
        row(shell, "Unpinned:", sizes, 3, SWT.DEFAULT);

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

    private static void row(Shell shell, String label, String[] items, int selected, int width) {
        Label caption = new Label(shell, SWT.NONE);
        caption.setText(label);
        caption.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

        // A RowLayout container, so the pinned width arrives as RowData rather than a
        // GridData hint -- the shape a toolbar contribution uses.
        Composite holder = new Composite(shell, SWT.NONE);
        holder.setLayout(new RowLayout(SWT.HORIZONTAL));

        CCombo combo = new CCombo(holder, SWT.BORDER | SWT.READ_ONLY);
        combo.setItems(items);
        combo.select(selected);
        if (width != SWT.DEFAULT) combo.setLayoutData(new RowData(width, SWT.DEFAULT));
    }
}

View on GitHub

Tooltip Snippet

CCombo  Tooltip Snippet

CCombo per-item tooltips (issue #610): tooltip strings are delivered through

{@code setData("org.eclipse.swt.custom.CCombo.itemToolTips", String[])} and rendered by the

Flutter dropdown. Open the dropdown and hover an item to see its documentation panel.

CComboTooltipSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * CCombo per-item tooltips (issue #610): tooltip strings are delivered through
 * {@code setData("org.eclipse.swt.custom.CCombo.itemToolTips", String[])} and rendered by the
 * Flutter dropdown. Open the dropdown and hover an item to see its documentation panel.
 */
public class CComboTooltipSnippet {

    private static final String ITEM_TOOLTIPS_KEY = "org.eclipse.swt.custom.CCombo.itemToolTips";

    public static void main(String[] args) {
        Config.useEquo(CCombo.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CCombo per-item tooltips");
        shell.setLayout(new GridLayout(1, false));

        Label label = new Label(shell, SWT.NONE);
        label.setText("Open the dropdown and hover an item to see its tooltip:");

        String[] items = {"Red", "Green", "Blue"};
        String[] tooltips = {
            "<h4>Red</h4><p>The colour <b>red</b> (#FF0000).</p><p><a href=\"https://example.com/red\">More</a></p>",
            "<h4>Green</h4><p>The colour <b>green</b> (#00FF00).</p><ul><li>a primary colour</li></ul>",
            "<h4>Blue</h4><p>The colour <b>blue</b> (#0000FF).</p><p><a href=\"https://example.com/blue\">More</a></p>",
        };

        CCombo combo = new CCombo(shell, SWT.BORDER | SWT.READ_ONLY);
        combo.setLayoutData(new GridData(260, SWT.DEFAULT));
        combo.setItems(items);
        combo.setData(ITEM_TOOLTIPS_KEY, tooltips);
        combo.select(0);

        shell.pack();
        shell.open();

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

View on GitHub