Skip to content

Composite Examples

Instances of this class are controls which are capable of containing other controls.

Button Snippet

Composite  Button Snippet

Example demonstrating CompositeButtonSnippet.

CompositeButtonSnippet.java
package dev.equo;

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

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

public class CompositeButtonSnippet {
    public static void main(String[] args) {
        Config.useEquo(Button.class);
        Config.useEquo(Label.class);

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Shell with Composite and Checkbox Example");
        shell.setSize(400, 300);

        // Configure shell layout
        shell.setLayout(new FillLayout());

        // Set shell background color
        shell.setBackground(display.getSystemColor(SWT.COLOR_RED));

        // Create a composite inside the shell
        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setLayout(new GridLayout(1, false));

        // Set composite background color
        composite.setBackground(display.getSystemColor(SWT.COLOR_MAGENTA));
        composite.setBackgroundMode(SWT.INHERIT_FORCE);

        // Add an explanatory label
        Label label = new Label(composite, SWT.NONE);
        label.setText("This is a Label inside a Composite inside a Shell:");
        label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

        // Create a checkbox inside the composite
        Button checkbox = new Button(composite, SWT.CHECK);
        checkbox.setText("Check this option!");
        checkbox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

        // Set checkbox background color explicitly
        checkbox.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));

        // Print background colors of all components
        Color shellColor = shell.getBackground();
        Color compositeColor = composite.getBackground();
        Color checkboxColor = checkbox.getBackground();

        System.out.println("Shell background color: " + shellColor);
        System.out.println("Composite background color: " + compositeColor);
        System.out.println("Checkbox background color: " + checkboxColor);

        // Add listener to checkbox
        checkbox.addSelectionListener(widgetSelectedAdapter(e -> {
            boolean isSelected = checkbox.getSelection();
            System.out.println("Checkbox inside the Composite was " + (isSelected ? "checked" : "unchecked") + "!");
            MessageBox messageBox = new MessageBox(shell, SWT.ICON_INFORMATION);
            messageBox.setText("Information");
            messageBox.setMessage("Checkbox " + (isSelected ? "checked" : "unchecked") + " successfully!");
            messageBox.open();
        }));

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

View on GitHub

Paint Listener Snippet

Composite  Paint Listener Snippet

A Composite — not a Canvas — that draws its whole content from a PaintListener, the shape a

colour-scale editor has: a gradient with a ruler down one side, and a hidden inline Text editor

that pops over a value on demand.

The two panels differ in exactly one thing: the right one owns a child (the editor), the left one

does not. Both must paint identically. If only the left one paints, the composite's children

branch is dropping the draw operations Java emits.

Every other GC example here paints on a Canvas, which is why this path went unexercised.

CompositePaintListenerSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * A Composite — not a Canvas — that draws its whole content from a PaintListener, the shape a
 * colour-scale editor has: a gradient with a ruler down one side, and a hidden inline Text editor
 * that pops over a value on demand.
 *
 * The two panels differ in exactly one thing: the right one owns a child (the editor), the left one
 * does not. Both must paint identically. If only the left one paints, the composite's children
 * branch is dropping the draw operations Java emits.
 *
 * Every other GC example here paints on a Canvas, which is why this path went unexercised.
 */
public class CompositePaintListenerSnippet {

    private static final int PANEL_W = 260;
    private static final int PANEL_H = 460;

    public static void main(String[] args) {
        Config.forceEquo();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CompositePaintListenerSnippet");
        shell.setSize(620, 600);

        new Label(shell, SWT.NONE).setBounds(20, 12, 260, 20);
        Label left = new Label(shell, SWT.NONE);
        left.setText("no children");
        left.setBounds(20, 12, 260, 20);
        Label right = new Label(shell, SWT.NONE);
        right.setText("one child (the inline editor)");
        right.setBounds(310, 12, 260, 20);

        Composite childless = new Composite(shell, SWT.NONE);
        childless.setBounds(20, 36, PANEL_W, PANEL_H);
        childless.addPaintListener(e -> drawScale(e.gc, "no children"));

        Composite withChild = new Composite(shell, SWT.NONE);
        withChild.setBounds(310, 36, PANEL_W, PANEL_H);
        withChild.addPaintListener(e -> drawScale(e.gc, "with child"));

        // The inline editor: present, hidden, and small — the only structural difference between
        // the two panels, and enough to route this one down the other build branch.
        Text editor = new Text(withChild, SWT.BORDER);
        editor.setBounds(4, 4, 57, 22);
        editor.setText("255.0");
        editor.setVisible(false);

        Button redraw = new Button(shell, SWT.PUSH);
        redraw.setText("redraw() both");
        redraw.setBounds(20, 508, 180, 30);
        redraw.addListener(SWT.Selection, e -> {
            childless.redraw();
            withChild.redraw();
        });

        Button toggleEditor = new Button(shell, SWT.PUSH);
        toggleEditor.setText("toggle editor");
        toggleEditor.setBounds(210, 508, 180, 30);
        toggleEditor.addListener(SWT.Selection, e -> editor.setVisible(!editor.getVisible()));

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

    /** Greyscale ramp, a ruler with labelled ticks, and a rotated caption. */
    private static void drawScale(GC gc, String caption) {
        int rampX = 60, rampW = 120;
        for (int y = 0; y < PANEL_H; y++) {
            int v = (int) (255.0 * y / PANEL_H);
            Color c = new Color(gc.getDevice(), v, v, v);
            gc.setBackground(c);
            gc.fillRectangle(rampX, y, rampW, 1);
            c.dispose();
        }

        Color black = new Color(gc.getDevice(), 0, 0, 0);
        gc.setForeground(black);
        gc.drawRectangle(rampX, 0, rampW, PANEL_H - 1);
        for (int i = 0; i <= 10; i++) {
            int y = i * (PANEL_H - 1) / 10;
            gc.drawLine(rampX - 8, y, rampX, y);
            gc.drawText(String.valueOf(i * 25.5), 4, Math.min(y, PANEL_H - 16), true);
        }
        gc.drawText(caption, rampX + rampW + 8, 4, true);
        black.dispose();
    }
}

View on GitHub

Reparent Snippet

Composite  Reparent Snippet

Moves nested content the way an RCP workbench does: a part's composite into a hidden shell and

back, tree branches collapsed and expanded, tabs switched, a part hidden and shown. Each step

first names itself in the shell title, then acts.

CompositeReparentSnippet.java
package dev.equo.internal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Moves nested content the way an RCP workbench does: a part's composite into a hidden shell and
 * back, tree branches collapsed and expanded, tabs switched, a part hidden and shown. Each step
 * first names itself in the shell title, then acts.
 */
public class CompositeReparentSnippet {

    static final int DEPTH = 4;

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("step-0-load");
        shell.setLayout(new GridLayout(2, true));

        Composite host = new Composite(shell, SWT.BORDER);
        host.setLayout(new FillLayout());
        Composite part = nested(host, "part");
        Shell limbo = new Shell(shell, SWT.NONE);

        Tree tree = new Tree(shell, SWT.BORDER);
        TreeItem root = new TreeItem(tree, SWT.NONE);
        root.setText("root");
        for (int i = 0; i < 5; i++) {
            TreeItem a = new TreeItem(root, SWT.NONE);
            a.setText("a" + i);
            for (int j = 0; j < 5; j++) {
                TreeItem b = new TreeItem(a, SWT.NONE);
                b.setText("a" + i + "b" + j);
                for (int k = 0; k < 3; k++) new TreeItem(b, SWT.NONE).setText("a" + i + "b" + j + "c" + k);
                b.setExpanded(true);
            }
            a.setExpanded(true);
        }
        root.setExpanded(true);

        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        for (int t = 0; t < 3; t++) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText("tab" + t);
            Composite body = new Composite(folder, SWT.NONE);
            body.setLayout(new FillLayout());
            nested(body, "tab" + t);
            item.setControl(body);
        }
        folder.setSelection(0);

        Composite toggled = new Composite(shell, SWT.BORDER);
        toggled.setLayout(new FillLayout());
        nested(toggled, "toggled");

        shell.setSize(1200, 800);
        shell.open();

        Runnable[] steps = {
            () -> { part.setParent(limbo); host.layout(true, true); },
            () -> { part.setParent(host); host.layout(true, true); },
            () -> { part.setParent(limbo); host.layout(true, true); },
            () -> { part.setParent(host); host.layout(true, true); },
            () -> root.setExpanded(false),
            () -> root.setExpanded(true),
            () -> root.setExpanded(false),
            () -> root.setExpanded(true),
            () -> folder.setSelection(1),
            () -> folder.setSelection(0),
            () -> folder.setSelection(2),
            () -> folder.setSelection(0),
            () -> toggled.setVisible(false),
            () -> toggled.setVisible(true),
        };
        String[] names = {"reparent-out", "reparent-in", "reparent-out", "reparent-in",
            "collapse", "expand", "collapse", "expand",
            "tab1", "tab0", "tab2", "tab0", "hide", "show"};
        int start = Integer.getInteger("delivery.startDelay", 20000);
        for (int s = 0; s < steps.length; s++) {
            int n = s;
            display.timerExec(start + n * 4000, () -> shell.setText("step-" + (n + 1) + "-" + names[n]));
            display.timerExec(start + n * 4000 + 500, () -> {
                System.out.println("step " + (n + 1) + " " + names[n]);
                steps[n].run();
            });
        }

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

    /** A chain of {@link #DEPTH} composites, each with a label and a toolbar, ending in a button. */
    static Composite nested(Composite parent, String name) {
        Composite top = null;
        Composite at = parent;
        for (int d = 0; d < DEPTH; d++) {
            Composite c = new Composite(at, SWT.NONE);
            c.setLayout(new GridLayout(1, false));
            new Label(c, SWT.NONE).setText(name + " level " + d);
            ToolBar bar = new ToolBar(c, SWT.FLAT);
            for (int i = 0; i < 3; i++) new ToolItem(bar, SWT.PUSH).setText(name + d + "." + i);
            if (top == null) top = c;
            at = c;
        }
        new Button(at, SWT.PUSH).setText(name + " leaf");
        return top;
    }
}

View on GitHub

Snippet

Composite  Snippet

Demonstrates the use of Composite with GridLayout and various GridData alignment options.

Shows how to position buttons using different horizontal and vertical alignment combinations.

CompositeSnippet.java
package dev.equo;

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

/**
 * Demonstrates the use of Composite with GridLayout and various GridData alignment options.
 * Shows how to position buttons using different horizontal and vertical alignment combinations.
 */
public class CompositeSnippet {
    public static void main(String[] args) {
        Config.useEquo(Composite.class);
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Composite Snippet");
        shell.setLayout(new FillLayout());

        Composite comp = createButtons(shell);
        new Label(comp, SWT.BORDER);
        comp = createButtons(comp);
        comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

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

    private static Composite createButtons(Composite parent) {
        Composite composite = new Composite(parent, SWT.BORDER);
        composite.setLayout(new GridLayout(4, false));

        Button b = new Button(composite, SWT.PUSH);
        b.setText("LEFT, TOP");
        b.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("LEFT, CENTER");
        b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("LEFT, BOTTOM");
        b.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("LEFT, FILL");
        b.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("CENTER, TOP");
        b.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("CENTER, CENTER");
        b.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("CENTER, BOTTOM");
        b.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("CENTER, FILL");
        b.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("RIGHT, TOP");
        b.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("RIGHT, CENTER");
        b.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("RIGHT, BOTTOM");
        b.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("RIGHT, FILL");
        b.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("FILL, TOP");
        b.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("FILL, CENTER");
        b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("FILL, BOTTOM");
        b.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1));
        b = new Button(composite, SWT.PUSH);
        b.setText("FILL, FILL");
        b.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        return composite;
    }
}

View on GitHub