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

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

Simple Snippet

CCombo  Simple Snippet

CCombo SIMPLE style - Shows list always visible (not dropdown)

Key difference: SIMPLE shows the list permanently below the text field

CComboSimpleSnippet.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 SIMPLE style - Shows list always visible (not dropdown)
 * Key difference: SIMPLE shows the list permanently below the text field
 */
public class CComboSimpleSnippet {
    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 SIMPLE Style");
        shell.setLayout(new GridLayout(1, false));

        String[] items = {"Red", "Green", "Blue", "Yellow", "Orange", "Purple", "Pink", "Brown"};

        // SIMPLE Read-Only CCombo
        Group simpleReadOnlyGroup = new Group(shell, SWT.NONE);
        simpleReadOnlyGroup.setText("SIMPLE + READ_ONLY");
        simpleReadOnlyGroup.setLayout(new GridLayout(2, false));
        simpleReadOnlyGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Label label1 = new Label(simpleReadOnlyGroup, SWT.NONE);
        label1.setText("Select from list:");

        CCombo simpleReadOnly = new CCombo(simpleReadOnlyGroup, SWT.SIMPLE | SWT.BORDER | SWT.READ_ONLY);
        simpleReadOnly.setLayoutData(new GridData(200, 150));
        simpleReadOnly.setItems(items);
        simpleReadOnly.select(0);

        // SIMPLE Editable CCombo
        Group simpleEditableGroup = new Group(shell, SWT.NONE);
        simpleEditableGroup.setText("SIMPLE + Editable");
        simpleEditableGroup.setLayout(new GridLayout(2, false));
        simpleEditableGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

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

        CCombo simpleEditable = new CCombo(simpleEditableGroup, SWT.SIMPLE | SWT.BORDER);
        simpleEditable.setLayoutData(new GridData(200, 150));
        simpleEditable.setItems(items);
        simpleEditable.setText("Type custom...");

        // Display group
        Group displayGroup = new Group(shell, SWT.NONE);
        displayGroup.setText("Current Selection");
        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("Selected:");

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

        // Add listeners
        simpleReadOnly.addSelectionListener(widgetSelectedAdapter(e -> {
            currentText.setText(simpleReadOnly.getText());
            System.out.println("Read-Only selected: " + simpleReadOnly.getText());
        }));

        simpleEditable.addSelectionListener(widgetSelectedAdapter(e -> {
            currentText.setText(simpleEditable.getText());
            System.out.println("Editable selected: " + simpleEditable.getText());
        }));

        simpleEditable.addModifyListener(e -> {
            System.out.println("Text modified: " + simpleEditable.getText());
        });

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

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

View on GitHub