Skip to content

CTabFolder Examples

Instances of this class implement the notebook user interface metaphor.

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

Snippet

CTabFolder  Snippet

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

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

With Content Snippet

CTabFolder  With Content Snippet

Example demonstrating CTabFolderWithContentSnippet.

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.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) {
        Config.forceEquo();
        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");

        Text text = new Text(folder, 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(text);

        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

With Top Right Tool Bar Snippet

CTabFolder  With Top Right Tool Bar Snippet

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.

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