An independent editorial resource for Java practitioners.

Browse the discussion resource

Java Chronicles

Stored discussion / SWT

SWT: Fancy Tabs

A preserved technical thread examining SWT tab controls: the standard TabFolder, the more flexible CTabFolder, and practical options for close controls, placement, selection, and visual treatment.

Original discussion: 29 December 2004Replies preserved: 12Reading time: 8 minutes
Opening note29 December 2004

Choosing a more capable tab folder

SWT provides org.eclipse.swt.widgets.TabFolder for conventional tabbed interfaces. It maps closely to native platform controls, which makes it familiar but can also limit presentation and behavior to what each operating system offers.

For interfaces that need controls on tabs, close affordances, alternate positioning, or a more Eclipse-like appearance, SWT also provides org.eclipse.swt.custom.CTabFolder. Its setup remains close to the standard widget, making it a practical substitute when an application needs more control.

A minimal standard tab folder

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

TabFolder tabs = new TabFolder(shell, SWT.NONE);
for (int i = 0; i < 6; i++) {
    TabItem item = new TabItem(tabs, SWT.NONE);
    item.setText("Tab Item " + i);
    Text text = new Text(tabs, SWT.BORDER | SWT.MULTI);
    text.setText("Content for Item " + i);
    item.setControl(text);
}

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

The custom folder follows the same basic shape. Replace the folder and item classes, then layer in the options the interface needs.

Moving to CTabFolder

CTabFolder tabs = new CTabFolder(shell, SWT.NONE);

for (int i = 0; i < 6; i++) {
    CTabItem item = new CTabItem(tabs, SWT.NONE);
    item.setText("Tab Item " + i);
    Text text = new Text(tabs, SWT.BORDER | SWT.MULTI);
    text.setText("Content for Item " + i);
    item.setControl(text);
}
resource note. The examples are retained as historical SWT guidance. Readers applying the pattern today should check current SWT documentation and their target platform behavior before standardising an interface.

Useful configuration points

Close controls can be enabled with SWT.CLOSE. The visibility of close affordances on unselected tabs can be adjusted with setUnselectedCloseVisible(). For a bottom-oriented tab strip, combine the style flags with SWT.BOTTOM.

CTabFolder tabs = new CTabFolder(
    shell, SWT.CLOSE | SWT.BOTTOM
);
tabs.setUnselectedCloseVisible(false);
tabs.setSimple(false);

The final setting above requests a more shaped tab presentation. When tab labels exceed available space, the custom folder also offers an overflow affordance, while the standard folder uses platform-oriented navigation behavior.

Practical takeaway

Choose TabFolder when native conventions are the priority and the interface is uncomplicated. Choose CTabFolder when the application benefits from a richer tab treatment, but keep interaction predictable: establish the initial selection deliberately, handle selection changes at the folder level, and test the result on the environments you support.

Discussion record

Twelve follow-up notes from the resource.

The responses below condense recurring themes from the preserved conversation without reproducing participant names or profile material.

01

Set an initial selection explicitly

One implementation note observed that an explicit setSelection(0) call can make the intended first tab clear when moving between the standard and custom folder controls.

02

Visual shading is a separate concern

Several readers were interested in deeper visual styling. SWT color and selection-background APIs were identified as the relevant area to examine, alongside platform-specific rendering limits.

03

Multi-row tabs need careful reconsideration

A question about wrapping a large number of labels illustrated a broader interface issue: overflow, shorter labels, or another navigation pattern can be easier to maintain than densely stacked tab rows.

04

Listen for selection at the folder level

Questions about reacting to a clicked tab point toward selection events on the folder. Application behavior can then be updated from the selected item rather than attaching unsupported assumptions to each tab item.

Add a reply

Keep reading

Continue from this SWT discussion into adjacent forum categories and longer-form resource material.

resource contact

Questions about this record?

For resource access and general site questions, write to info@javachronicles.en-html.org.

Contact Java Chronicles