Java Tutorial/SWT/TabFolder
Add an event listener to write the selected tab to stdout
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabFolderSelectionEvent {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
for (int i = 0; i < 6; 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);
}
// Add an event listener to write the selected tab to stdout
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
System.out.println(tabFolder.getSelection()[0].getText() + " selected");
}
});
tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create TabFolder and add TabItem
- SWT divides its tab implementation into two classes: TabFolder and TabItem.
- TabFolders, which aren"t visible, contain TabItems.
- To create a tabbed interface, create a TabFolder with a Shell as its parent, and create TabItems as children of the TabFolder.
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabFolderTabItem {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
for (int i = 0; i < 6; 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);
}
tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set selected tab
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabItemSelection {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
for (int i = 0; i < 6; 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);
}
tabFolder.setSelection(2);
tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}