Skip to content

CTabFolder Examples

Instances of this class implement the notebook user interface metaphor.

Chevron Fit Snippet

CTabFolder  Chevron Fit Snippet

A view stack of closable icon tabs that starts too narrow for them and is then widened step by

step, as a dragged sash does, until every tab fits. Run with -Dswt.evolve.ctabfolder_tab_scroll=false:

once every tab shows, the chevron must go.

CTabFolderChevronFitSnippet.java
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * A view stack of closable icon tabs that starts too narrow for them and is then widened step by
 * step, as a dragged sash does, until every tab fits. Run with -Dswt.evolve.ctabfolder_tab_scroll=false:
 * once every tab shows, the chevron must go.
 */
public class CTabFolderChevronFitSnippet {
    static final String[] LABELS = {"Search", "Console", "Outline", "Problems", "Tasks", "Progress", "History",
            "Markers", "Javadoc"};

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolderChevronFitSnippet");
        shell.setLayout(new GridLayout());
        CTabFolder folder = new CTabFolder(shell, SWT.BORDER | SWT.CLOSE);
        folder.setMinimumCharacters(1);
        folder.setMinimizeVisible(true);
        folder.setMaximizeVisible(true);
        Image icon = new Image(display, 16, 16);
        GC gc = new GC(icon);
        gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_BLUE));
        gc.fillOval(2, 2, 12, 12);
        gc.dispose();
        for (String label : LABELS) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText(label);
            item.setImage(icon);
            Text body = new Text(folder, SWT.MULTI);
            body.setText("Content for " + label);
            item.setControl(body);
        }
        folder.setSelection(0);
        GridData data = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
        data.widthHint = 200;
        folder.setLayoutData(data);
        shell.setSize(800, 300);
        shell.open();
        Runnable widen = new Runnable() {
            @Override
            public void run() {
                if (shell.isDisposed() || data.widthHint >= 760) return;
                data.widthHint += 10;
                shell.layout(true, true);
                display.timerExec(300, this);
            }
        };
        display.timerExec(1000, widen);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        icon.dispose();
        display.dispose();
    }
}

View on GitHub

Close Tab Snippet

CTabFolder  Close Tab Snippet

Snippet to reproduce CTabFolder/CTabItem issues:

  1. Tab font color appears inactive (gray) even when selected

  2. After closing a tab via the close button, the tab stays visible

Steps to reproduce:

  • Click on different tabs to observe font color

  • Click the X button on a tab to close it

  • Observe if the tab disappears correctly

CTabFolderCloseTabSnippet.java
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Snippet to reproduce CTabFolder/CTabItem issues:
 * 1. Tab font color appears inactive (gray) even when selected
 * 2. After closing a tab via the close button, the tab stays visible
 *
 * Steps to reproduce:
 * - Click on different tabs to observe font color
 * - Click the X button on a tab to close it
 * - Observe if the tab disappears correctly
 */
public class CTabFolderCloseTabSnippet {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolder Close Tab Bug");
        shell.setLayout(new GridLayout());

        CTabFolder folder = new CTabFolder(shell, SWT.BORDER | SWT.CLOSE);
        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // Create tabs with close button
        for (int i = 0; i < 5; i++) {
            CTabItem item = new CTabItem(folder, SWT.CLOSE);
            item.setText("Tab " + i);
            Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL);
            text.setText("Content of Tab " + i + "\n\nClick the X to close this tab.");
            item.setControl(text);
        }

        // Select first tab
        folder.setSelection(0);

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

View on GitHub

Deferred Page Layout Snippet

CTabFolder  Deferred Page Layout Snippet

A CTabFolder whose selected page is laid out a turn after the folder itself is resized, as a

workbench does when it defers layout during a window resize.

Maximise or resize the window: the page (bordered) must fill the tab body every time, without clicking a tab. ??? example "CTabFolderDeferredPageLayoutSnippet.java"

package dev.equo;

import dev.equo.swt.Config;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * A CTabFolder whose selected page is laid out a turn after the folder itself is resized, as a
 * workbench does when it defers layout during a window resize.
 *
 * <p>Maximise or resize the window: the page (bordered) must fill the tab body every time,
 * without clicking a tab.
 */
public class CTabFolderDeferredPageLayoutSnippet {

    public static void main(String[] args) {
        Config.forceEquo();
        Display display = new Display();

        Shell shell = new Shell(display);
        shell.setText("CTabFolder deferred page layout");
        shell.setLayout(new GridLayout());

        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        for (int i = 1; i <= 2; i++) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText("Tab " + i);
            Composite page = new Composite(folder, SWT.BORDER);
            page.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
            page.setLayout(new FillLayout());
            Label label = new Label(page, SWT.WRAP);
            label.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
            label.setText("Page " + i + ": this page must fill the whole tab body.");
            item.setControl(page);
        }
        folder.setSelection(0);

        shell.addListener(SWT.Resize, e -> {
            if (folder.isLayoutDeferred()) return;
            folder.setLayoutDeferred(true);
            display.timerExec(150, () -> {
                if (folder.isDisposed()) return;
                folder.setLayoutDeferred(false);
                System.out.println("folder=" + folder.getBounds() + " page=" + folder.getSelection().getControl().getBounds());
            });
        });

        shell.setSize(500, 320);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderDeferredPageLayoutSnippet.java){ .md-button } ### Drag Drop Snippet [![CTabFolder Drag Drop Snippet](assets/screenshots/CTabFolderDragDropSnippet.png)](assets/screenshots/CTabFolderDragDropSnippet.png){.glightbox} Drags a CTabItem from one CTabFolder onto another.

CTabFolder has no built-in cross-folder move: an application builds one on top of DragSource/DropTarget, the way the Eclipse workbench moves a view between stacks — reparent the tab's content Control and create a CTabItem in the target folder. A folder that has a DropTarget owns every drop on its tab row, including a move within itself, so this snippet handles both cases through the same {@link #moveTab} call. ??? example "CTabFolderDragDropSnippet.java"

package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Drags a CTabItem from one CTabFolder onto another.
 *
 * <p>CTabFolder has no built-in cross-folder move: an application builds one on top of
 * DragSource/DropTarget, the way the Eclipse workbench moves a view between stacks — reparent
 * the tab's content Control and create a CTabItem in the target folder. A folder that has a
 * DropTarget owns every drop on its tab row, including a move within itself, so this snippet
 * handles both cases through the same {@link #moveTab} call.
 */
public class CTabFolderDragDropSnippet {

    /**
     * The tab in flight. TextTransfer only has to carry something for the type negotiation; the
     * item itself never leaves the process, the same split JFace's LocalSelectionTransfer makes.
     */
    private static CTabItem dragged;

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

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolderDragDropSnippet");
        shell.setLayout(new FillLayout(SWT.HORIZONTAL));
        shell.setSize(700, 300);

        addDragAndDrop(createFolder(shell, "Left", 3));
        addDragAndDrop(createFolder(shell, "Right", 2));

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

    private static CTabFolder createFolder(Shell shell, String prefix, int count) {
        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        for (int i = 0; i < count; i++) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText(prefix + " " + (i + 1));
            Label content = new Label(folder, SWT.CENTER);
            content.setText("Content of " + item.getText());
            item.setControl(content);
        }
        folder.setSelection(0);
        return folder;
    }

    private static void addDragAndDrop(CTabFolder folder) {
        DragSource source = new DragSource(folder, DND.DROP_MOVE);
        source.setTransfer(TextTransfer.getInstance());
        source.addDragListener(new DragSourceAdapter() {
            @Override
            public void dragStart(DragSourceEvent event) {
                dragged = folder.getSelection();
                event.doit = dragged != null;
            }

            @Override
            public void dragSetData(DragSourceEvent event) {
                event.data = dragged != null ? dragged.getText() : "";
            }
        });

        DropTarget target = new DropTarget(folder, DND.DROP_MOVE);
        target.setTransfer(TextTransfer.getInstance());
        target.addDropListener(new DropTargetAdapter() {
            @Override
            public void drop(DropTargetEvent event) {
                if (dragged == null || dragged.isDisposed()) return;
                // event.item is the tab under the cursor; no tab there means the drop landed
                // past the last one, so the moved tab goes to the end.
                int index = event.item instanceof CTabItem hovered
                        ? folder.indexOf(hovered)
                        : folder.getItemCount();
                moveTab(dragged, folder, index);
                dragged = null;
            }
        });
    }

    private static void moveTab(CTabItem item, CTabFolder target, int index) {
        CTabFolder source = item.getParent();
        String text = item.getText();
        Control content = item.getControl();
        int removed = source.indexOf(item);

        // Detach the content before disposing the item so the old folder stops laying it out
        // (destroying the selected item moves the selection, which hides its control).
        item.setControl(null);
        item.dispose();

        if (source == target && removed < index) index--;
        if (content != null && !content.isDisposed()) content.setParent(target);

        CTabItem moved = new CTabItem(target, SWT.NONE, Math.max(0, Math.min(index, target.getItemCount())));
        moved.setText(text);
        moved.setControl(content);
        target.setSelection(moved);
        target.layout(true, true);
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderDragDropSnippet.java){ .md-button } ### Narrow Min Max Snippet [![CTabFolder Narrow Min Max Snippet](assets/screenshots/CTabFolderNarrowMinMaxSnippet.png)](assets/screenshots/CTabFolderNarrowMinMaxSnippet.png){.glightbox} {@link CTabFolderNarrowSnippet} with the minimize and maximize buttons shown, which take their width from the tab row as in SWT. ??? example "CTabFolderNarrowMinMaxSnippet.java"
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * {@link CTabFolderNarrowSnippet} with the minimize and maximize buttons shown, which take their
 * width from the tab row as in SWT.
 */
public class CTabFolderNarrowMinMaxSnippet {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolderNarrowMinMaxSnippet");
        shell.setLayout(new GridLayout());
        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setMinimumCharacters(3);
        folder.setMinimizeVisible(true);
        folder.setMaximizeVisible(true);
        GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
        data.widthHint = 240;
        data.heightHint = 120;
        folder.setLayoutData(data);
        for (String label : new String[] {"Overview", "Configuration", "Dependencies", "Run History"}) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText(label);
            Text body = new Text(folder, SWT.MULTI);
            body.setText("Content for " + label);
            item.setControl(body);
        }
        folder.setSelection(0);
        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderNarrowMinMaxSnippet.java){ .md-button } ### Narrow Snippet [![CTabFolder Narrow Snippet](assets/screenshots/CTabFolderNarrowSnippet.png)](assets/screenshots/CTabFolderNarrowSnippet.png){.glightbox} A CTabFolder too narrow for its tabs at full width. With -Dswt.evolve.ctabfolder_tab_scroll=false the tabs shrink towards minimumCharacters as in SWT; by default the strip scrolls instead. ??? example "CTabFolderNarrowSnippet.java"
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * A CTabFolder too narrow for its tabs at full width. With -Dswt.evolve.ctabfolder_tab_scroll=false
 * the tabs shrink towards minimumCharacters as in SWT; by default the strip scrolls instead.
 */
public class CTabFolderNarrowSnippet {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolderNarrowSnippet");
        shell.setLayout(new GridLayout());
        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setMinimumCharacters(3);
        GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
        data.widthHint = 240;
        data.heightHint = 120;
        folder.setLayoutData(data);
        for (String label : new String[] {"Overview", "Configuration", "Dependencies", "Run History"}) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText(label);
            Text body = new Text(folder, SWT.MULTI);
            body.setText("Content for " + label);
            item.setControl(body);
        }
        folder.setSelection(0);
        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderNarrowSnippet.java){ .md-button } ### Snippet [![CTabFolder Snippet](assets/screenshots/CTabFolderSnippet.png)](assets/screenshots/CTabFolderSnippet.png){.glightbox} Demonstrates the use of CTabFolder (Custom Tab Folder) widget in SWT. This example creates a tab folder with 8 tabs, each containing a text area with sample content. The tab folder includes: - Minimize/maximize buttons in the tab bar - Support for minimizing, maximizing, and restoring the tab folder - Multi-line text content in each tab with scrollbars - Custom styling with borders and proper layout management ??? example "CTabFolderSnippet.java"
package dev.equo;
import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Demonstrates the use of CTabFolder (Custom Tab Folder) widget in SWT.
 * This example creates a tab folder with 8 tabs, each containing a text area with sample content.
 * The tab folder includes:
 * - Minimize/maximize buttons in the tab bar
 * - Support for minimizing, maximizing, and restoring the tab folder
 * - Multi-line text content in each tab with scrollbars
 * - Custom styling with borders and proper layout management
 */
public class CTabFolderSnippet {
    public static void main (String [] args) {
        Display display = new Display ();

        final Shell shell = new Shell (display);
        shell.setText("CTabFolderSnippet");
        shell.setLayout(new GridLayout());
        final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        folder.setSimple(false);
        folder.setUnselectedImageVisible(false);
        folder.setUnselectedCloseVisible(false);
        for (int i = 0; i < 8; i++) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText("Item "+i);
            Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
            text.setText("Text for item "+i+"\n\none, two, three\n\nabcdefghijklmnop");
            item.setControl(text);
        }
        shell.setSize(350, 300);
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderSnippet.java){ .md-button } ### Tab Outline Snippet [![CTabFolder Tab Outline Snippet](assets/screenshots/CTabFolderTabOutlineSnippet.png)](assets/screenshots/CTabFolderTabOutlineSnippet.png){.glightbox} A bordered CTabFolder with several tabs, drawn the way an Eclipse part stack configures it.

Native SWT draws one outline: it runs around the whole folder, the tab strip included, climbs the selected tab's sides and top, and goes on along the strip's bottom edge on both sides of it. The selected tab is open towards the page below, so tab, strip and frame read as one shape.

Evolve draws that outline only with {@code -Dswt.use_swt_border=true}, which this snippet sets; without it a bordered folder keeps the theme's flat frame. ??? example "CTabFolderTabOutlineSnippet.java"

package dev.equo;

import dev.equo.swt.Config;
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.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * A bordered CTabFolder with several tabs, drawn the way an Eclipse part stack configures it.
 *
 * <p>Native SWT draws one outline: it runs around the whole folder, the tab strip included, climbs
 * the selected tab's sides and top, and goes on along the strip's bottom edge on both sides of it.
 * The selected tab is open towards the page below, so tab, strip and frame read as one shape.
 *
 * <p>Evolve draws that outline only with {@code -Dswt.use_swt_border=true}, which this snippet sets;
 * without it a bordered folder keeps the theme's flat frame.
 */
public class CTabFolderTabOutlineSnippet {

    public static void main(String[] args) {
        System.setProperty("swt.use_swt_border", "true");
        Config.forceEquo();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolder tab outline");
        shell.setLayout(new FillLayout());

        CTabFolder folder = new CTabFolder(shell, SWT.BORDER | SWT.CLOSE);
        folder.setSelectionBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
        folder.setTabHeight(24);
        for (String name : new String[] {"View A", "View B", "View C"}) {
            CTabItem item = new CTabItem(folder, SWT.NONE);
            item.setText(name);
            Label page = new Label(folder, SWT.NONE);
            page.setText(name + " content");
            page.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
            item.setControl(page);
        }
        folder.setSelection(0);

        shell.setSize(360, 160);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderTabOutlineSnippet.java){ .md-button } ### Top Right Reserve Space Snippet [![CTabFolder Top Right Reserve Space Snippet](assets/screenshots/CTabFolderTopRightReserveSpaceSnippet.png)](assets/screenshots/CTabFolderTopRightReserveSpaceSnippet.png){.glightbox} With several tabs open and a narrow window, the topRight toolbar floats on top of the tab row and can hide tabs underneath it with no way to reach them. Default behaviour, unchanged. Pass -Dswt.evolve.ctabfolder_topright_auto_hide=false to compare against the fix (toolbar always shown, reserving its own space instead of floating). ??? example "CTabFolderTopRightReserveSpaceSnippet.java"
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * With several tabs open and a narrow window, the topRight toolbar floats on top of
 * the tab row and can hide tabs underneath it with no way to reach them. Default
 * behaviour, unchanged. Pass -Dswt.evolve.ctabfolder_topright_auto_hide=false to
 * compare against the fix (toolbar always shown, reserving its own space instead of
 * floating).
 */
public class CTabFolderTopRightReserveSpaceSnippet {

    public static void main(String[] args) {
        Config.forceEquo();
        // Forced on for both runs of this snippet: makes the topRight toolbar
        // render without needing a live mouse hover even in the default (hover-only)
        // mode. ctabfolder_topright_auto_hide is the actual variable under
        // comparison -- pass -Dswt.evolve.ctabfolder_topright_auto_hide=false to
        // see the fix.
        System.setProperty("swt.evolve.ctabfolder_visible_controls", "true");
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolder TopRight Reserve-Space Snippet -- auto_hide="
                + Boolean.parseBoolean(System.getProperty("swt.evolve.ctabfolder_topright_auto_hide", "true")));
        shell.setLayout(new GridLayout());
        final Point pinnedSize = new Point(300, 250);
        shell.setSize(pinnedSize);
        // The desktop harness's DPI/monitor negotiation drifts the shell size upward
        // over the first couple seconds if left alone -- pin it back on every resize
        // so the narrow-window scenario this snippet exists to show actually holds.
        shell.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                if (!shell.getSize().equals(pinnedSize)) {
                    shell.setSize(pinnedSize);
                }
            }
        });

        CTabFolder tabFolder = new CTabFolder(shell, SWT.BORDER);
        tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Composite toolbarComposite = new Composite(tabFolder, SWT.NONE);
        FillLayout toolbarLayout = new FillLayout();
        toolbarLayout.marginHeight = 2;
        toolbarLayout.marginWidth = 4;
        toolbarComposite.setLayout(toolbarLayout);

        ToolBar toolBar = new ToolBar(toolbarComposite, SWT.FLAT);
        ToolItem itemA = new ToolItem(toolBar, SWT.PUSH);
        itemA.setText("Refresh");
        ToolItem itemB = new ToolItem(toolBar, SWT.PUSH);
        itemB.setText("Settings");

        tabFolder.setTopRight(toolbarComposite);

        for (int i = 1; i <= 8; i++) {
            CTabItem tab = new CTabItem(tabFolder, SWT.NONE);
            tab.setText("Tab " + i);
            Composite body = new Composite(tabFolder, SWT.NONE);
            body.setLayout(new FillLayout());
            new Label(body, SWT.NONE).setText("Content of tab " + i);
            tab.setControl(body);
        }

        // Selects the last tab -- the one most likely to end up hidden underneath
        // the toolbar overlay when the tabs don't all fit.
        tabFolder.setSelection(tabFolder.getItemCount() - 1);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderTopRightReserveSpaceSnippet.java){ .md-button } ### With Content Snippet [![CTabFolder With Content Snippet](assets/screenshots/CTabFolderWithContentSnippet.png)](assets/screenshots/CTabFolderWithContentSnippet.png){.glightbox} Example demonstrating CTabFolderWithContentSnippet. ??? example "CTabFolderWithContentSnippet.java"
package dev.equo;

import dev.equo.swt.Config;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class CTabFolderWithContentSnippet {

    private static void log(String message) {
        System.out.println("[DEBUG] " + message);
    }

    public static void main(String[] args) {
        System.setProperty("dev.equo.swt.debug", "true");
        Config.forceEquo();
//        Config.defaultToEquo();
        Display display = new Display();

        Shell shell = new Shell(display);
        shell.setText("CTabFolder Debug");
        shell.setLayout(new GridLayout());

        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        log("CTabFolder created, clientArea: " + folder.getClientArea());

        // Tab with content
        CTabItem item = new CTabItem(folder, SWT.NONE);
        item.setText("Tab 1");

        Composite body = new Composite(folder, SWT.BORDER);
        body.setLayout(new FillLayout());

        Text text = new Text(body, SWT.MULTI | SWT.V_SCROLL);
        text.setText("Tab 1 content\n\nLine 2\nLine 3");

        log("Before setControl - text bounds: " + text.getBounds());
        log("Before setControl - clientArea: " + folder.getClientArea());

        item.setControl(body);

        log("After setControl - text bounds: " + text.getBounds());
        log("After setControl - text visible: " + text.getVisible());

        folder.setSelection(0);

        log("After setSelection - text bounds: " + text.getBounds());
        log("After setSelection - text visible: " + text.getVisible());

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

        log("After open - clientArea: " + folder.getClientArea());
        log("After open - text bounds: " + text.getBounds());
        log("After open - text visible: " + text.getVisible());

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderWithContentSnippet.java){ .md-button } ### With Top Right Tool Bar Snippet [![CTabFolder With Top Right Tool Bar Snippet](assets/screenshots/CTabFolderWithTopRightToolBarSnippet.png)](assets/screenshots/CTabFolderWithTopRightToolBarSnippet.png){.glightbox} Demonstrates CTabFolder with an integrated toolbar in the top-right corner. This example shows how to use setTopRight() method to embed a custom toolbar with different types of tool items including icons and tooltips. ??? example "CTabFolderWithTopRightToolBarSnippet.java"
package dev.equo;

import dev.equo.swt.Config;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * Demonstrates CTabFolder with an integrated toolbar in the top-right corner.
 * This example shows how to use setTopRight() method to embed a custom toolbar with different types of tool items including icons and tooltips.
 */
public class CTabFolderWithTopRightToolBarSnippet {

    public static void main(String[] args) throws IOException {
        Config.forceEquo();
        System.setProperty("swt.evolve.ctabfolder_visible_controls", "true");
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolder with Toolbar TopRight Snippet");
        shell.setSize(400, 100);
        shell.setLayout(new FillLayout());

        CTabFolder tabFolder = new CTabFolder(shell, SWT.BORDER);

        //tabFolder.setMinimizeVisible(true);
        //tabFolder.setMaximizeVisible(true);

        Composite toolbarComposite = new Composite(tabFolder, SWT.NONE);
        FillLayout toolbarLayout = new FillLayout();
        toolbarLayout.marginHeight = 2;
        toolbarLayout.marginWidth = 2;
        toolbarComposite.setLayout(toolbarLayout);

        ToolBar toolBar = new ToolBar(toolbarComposite, SWT.FLAT);

        ToolItem itemA = new ToolItem(toolBar, SWT.PUSH);
        itemA.setText("A");
        itemA.setToolTipText("Item A");

        ToolItem check = new ToolItem(toolBar, SWT.CHECK);
        check.setText("Check");
        check.setToolTipText("Check Item");
        check.setImage(new Image(display, getImagePath("synced.png")));

        ToolItem backward_forward = new ToolItem(toolBar, SWT.PUSH);
        backward_forward.setToolTipText("backwards forward");
        backward_forward.setImage(new Image(display, getImagePath("backward_nav.png")));
        backward_forward.setHotImage(new Image(display, getImagePath("forward_nav.png")));

        ToolItem view_menu = new ToolItem(toolBar, SWT.PUSH);
        view_menu.setToolTipText("view_menu");
        view_menu.setImage(new Image(display, getImagePath("view_menu.png")));

        tabFolder.setTopRight(toolbarComposite);

        CTabItem tab1 = new CTabItem(tabFolder, SWT.NONE);
        tab1.setText("Tab 1");

        CTabItem tab2 = new CTabItem(tabFolder, SWT.NONE);
        tab2.setText("Tab 2");

        tabFolder.setSelection(0);

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

    private static String getImagePath(String imageName) throws IOException {
        URL resourceUrl = CTabFolderWithTopRightToolBarSnippet.class.getResource("/" + imageName);
        if (resourceUrl == null) {
            throw new IllegalArgumentException("Resource not found: " + imageName);
        }

        if ("file".equals(resourceUrl.getProtocol())) {
            return Paths.get(resourceUrl.getPath()).toString();
        } else {
            InputStream is = CTabFolderWithTopRightToolBarSnippet.class.getResourceAsStream("/" + imageName);
            Path tmpDir = Files.createTempDirectory("swt-evolve-images-");
            Path tmpFile = tmpDir.resolve(imageName);
            assert is != null;
            Files.copy(is, tmpFile, StandardCopyOption.REPLACE_EXISTING);
            tmpFile.toFile().deleteOnExit();
            return tmpFile.toAbsolutePath().toString();
        }
    }


}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderWithTopRightToolBarSnippet.java){ .md-button } ### Wrapped Tool Bar Snippet [![CTabFolder Wrapped Tool Bar Snippet](assets/screenshots/CTabFolderWrappedToolBarSnippet.png)](assets/screenshots/CTabFolderWrappedToolBarSnippet.png){.glightbox} A view stack as a workbench builds it: a CTabFolder with a fixed tab height whose topRight tool bar (SWT.RIGHT | SWT.WRAP) does not fit beside the tabs, so the folder moves it to a row of its own under them and lays the page out under that row. ??? example "CTabFolderWrappedToolBarSnippet.java"
package dev.equo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * A view stack as a workbench builds it: a CTabFolder with a fixed tab height whose topRight tool
 * bar (SWT.RIGHT | SWT.WRAP) does not fit beside the tabs, so the folder moves it to a row of its
 * own under them and lays the page out under that row.
 */
public class CTabFolderWrappedToolBarSnippet {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("CTabFolderWrappedToolBarSnippet");
        shell.setLayout(new GridLayout());
        CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setTabHeight(40);
        folder.setMinimizeVisible(true);
        folder.setMaximizeVisible(true);
        GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
        data.widthHint = 300;
        data.heightHint = 200;
        folder.setLayoutData(data);

        Composite topRight = new Composite(folder, SWT.NONE);
        RowLayout rowLayout = new RowLayout();
        rowLayout.marginLeft = rowLayout.marginTop = rowLayout.marginRight = rowLayout.marginBottom = 0;
        topRight.setLayout(rowLayout);
        ToolBar toolBar = new ToolBar(topRight, SWT.FLAT | SWT.RIGHT);
        for (String label : new String[] {"Sort", "Filter", "Link", "Pin", "Menu"}) {
            new ToolItem(toolBar, SWT.PUSH).setText(label);
        }
        folder.setTopRight(topRight, SWT.RIGHT | SWT.WRAP);

        for (String label : new String[] {"Outline", "Properties", "Assistant", "Favorites"}) {
            CTabItem item = new CTabItem(folder, SWT.CLOSE);
            item.setText(label);
            Text body = new Text(folder, SWT.MULTI);
            body.setText("Content for " + label);
            item.setControl(body);
        }
        folder.setSelection(1);
        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
[View on GitHub :simple-github:](https://github.com/equodev/swt-evolve/blob/main/examples/src/main/java/dev/equo/CTabFolderWrappedToolBarSnippet.java){ .md-button }