Java SWT TabFolder and CTabFolder Tutorial with Examples
1. TabFolder and CTabFolder
In SWT, TabFolder is a subclass of Composite. Each TabFolder can contain one or more TabItems. At a time, the user only see one TabItem.

CTabFolder is a class expanded from class Composite, it is similar to TabFolder. CTabFolder contains one or more CTabItems, and you can close CTabItem.

2. TabFolder Example
No ADS
The example, one TabFolder with 2 Tabs.

TabFolderDemo.java
package org.o7planning.swt.tabfolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
public class TabFolderDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT TabFolder (o7planning.org)");
shell.setSize(450, 300);
shell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(shell, SWT.NONE);
// Tab 1
TabItem tab1 = new TabItem(folder, SWT.NONE);
tab1.setText("Tab 1");
// Create the HORIZONTAL SashForm.
SashForm sashForm = new SashForm(folder, SWT.HORIZONTAL);
new Button(sashForm, SWT.PUSH).setText("Left");
new Button(sashForm, SWT.PUSH).setText("Right");
sashForm.setWeights(new int[]{1,2});
tab1.setControl(sashForm);
// Tab 2
TabItem tab2 = new TabItem(folder, SWT.NONE);
tab2.setText("Tab 2");
Group group = new Group(folder, SWT.NONE);
group.setText("Group in Tab 2");
Button button = new Button(group, SWT.NONE);
button.setText("Button in Tab 2");
button.setBounds(10, 50, 130, 30);
Text text = new Text(group, SWT.BORDER);
text.setText("Text in Tab 2");
text.setBounds(10, 90, 200, 20);
tab2.setControl(group);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
3. CTabFolder Example
CTabFolder is a customizable tab-folder. CTabItems can use style SWT.CLOSE to allow the user to close the CTabItem.

CTabFolderDemo.java
package org.o7planning.swt.tabfolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTabFolderDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT CTabFolder (o7planning.org)");
shell.setSize(450, 300);
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.NONE);
// Tab 1
CTabItem tab1 = new CTabItem(folder, SWT.NONE);
tab1.setText("Tab 1");
// Create the HORIZONTAL SashForm.
SashForm sashForm = new SashForm(folder, SWT.HORIZONTAL);
new Button(sashForm, SWT.PUSH).setText("Left");
new Button(sashForm, SWT.PUSH).setText("Right");
sashForm.setWeights(new int[] { 1, 2 });
tab1.setControl(sashForm);
// Tab 2 with close icon
CTabItem tab2 = new CTabItem(folder, SWT.NONE | SWT.CLOSE);
tab2.setText("Tab 2");
Group group = new Group(folder, SWT.NONE);
group.setText("Group in Tab 2");
Button button = new Button(group, SWT.NONE);
button.setText("Button in Tab 2");
button.setBounds(10, 50, 130, 30);
Text text = new Text(group, SWT.BORDER);
text.setText("Text in Tab 2");
text.setBounds(10, 90, 200, 20);
tab2.setControl(group);
// Tab 3 with close icon
CTabItem tab3 = new CTabItem(folder, SWT.NONE | SWT.CLOSE);
tab3.setText("Tab 3");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
4. TabFolder & Event
No ADS

TabFolderEventsExample.java
package org.o7planning.swt.tabfolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabFolderEventsExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT TabFolder events (o7planning.org)");
shell.setSize(450, 300);
shell.setLayout(null);
Button buttonNext = new Button(shell, SWT.NONE);
buttonNext.setText("Next >");
buttonNext.setBounds(10, 10, 50, 25);
// Log
Label labelLog = new Label(shell, SWT.NONE);
labelLog.setBounds(10, 40, 400, 20);
// TabFolder
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
tabFolder.setBounds(10, 70, 400, 150);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(tabFolder, SWT.NONE);
item.setText("TabItem " + i);
Button button = new Button(tabFolder, SWT.PUSH);
button.setText("Page " + i);
item.setControl(button);
}
// Events
tabFolder.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
TabItem item = tabFolder.getSelection()[0];
labelLog.setText(item.getText() + " selected!");
}
});
buttonNext.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
int idx = tabFolder.getSelectionIndex();
if (idx + 1 == tabFolder.getItemCount()) {
idx = -1;
}
tabFolder.setSelection(idx + 1);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
5. CTabFolder & Event

CTabFolderEventsExample.java
package org.o7planning.swt.tabfolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CTabFolderEventsExample {
private static int nextIndex = 6;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT CTabFolder events (o7planning.org)");
shell.setSize(450, 300);
shell.setLayout(null);
Button buttonAdd = new Button(shell, SWT.NONE);
buttonAdd.setText("Add Tab");
buttonAdd.setBounds(10, 10, 70, 25);
Button buttonClose = new Button(shell, SWT.NONE);
buttonClose.setText("Close Tab");
buttonClose.setBounds(90, 10, 80, 25);
// CTabFolder
CTabFolder ctabFolder = new CTabFolder(shell, SWT.NONE);
ctabFolder.setBounds(10, 70, 400, 150);
for (int i = 0; i < 5; i++) {
CTabItem item = new CTabItem(ctabFolder, SWT.CLOSE);
item.setText("TabItem " + i);
Button button = new Button(ctabFolder, SWT.PUSH);
button.setText("Page " + i);
item.setControl(button);
}
ctabFolder.setSelection(0);
// Add Tab
buttonAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
int idx = ctabFolder.getSelectionIndex();
CTabItem item = new CTabItem(ctabFolder, SWT.CLOSE, idx + 1);
item.setText("TabItem " + nextIndex++);
}
});
// Close Tab
buttonClose.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
CTabItem item = ctabFolder.getSelection();
if (item != null) {
item.dispose();
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
No ADS
Java SWT Tutorials
- Java SWT FillLayout Tutorial with Examples
- Java SWT RowLayout Tutorial with Examples
- Java SWT SashForm Tutorial with Examples
- Java SWT Label Tutorial with Examples
- Java SWT Button Tutorial with Examples
- Java SWT Toggle Button Tutorial with Examples
- Java SWT Radio Button Tutorial with Examples
- Java SWT Text Tutorial with Examples
- Java SWT Password Field Tutorial with Examples
- Java SWT Link Tutorial with Examples
- Programming Java Desktop Application Using SWT
- Java SWT Combo Tutorial with Examples
- Java SWT Spinner Tutorial with Examples
- Java SWT Slider Tutorial with Examples
- Java SWT Scale Tutorial with Examples
- Java SWT ProgressBar Tutorial with Examples
- Java SWT TabFolder and CTabFolder Tutorial with Examples
- Java SWT List Tutorial with Examples
Show More