openplanning

Hướng dẫn và ví dụ Java SWT TabFolder và CTabFolder

  1. TabFolder và CTabFolder
  2. Ví dụ TabFolder
  3. Ví dụ CTabFolder
  4. TabFolder & Event
  5. CTabFolder & Event

1. TabFolder và CTabFolder

Trong SWT, TabFolder là một class con của Composite. Mỗi TabFolder có thể chứa nhiều TabItem. Trong một thời điểm người dùng chỉ có thể nhìn thấy một TabItem.
CTabFolder là một class mở rộng từ class Composite, nó tương tự với TabFolder. CTabFolder chứa một hoặc nhiều CTabItem, và bạn có thể đóng các CTabItem.

2. Ví dụ TabFolder

Ví dụ dưới đây, một TabFolder với 2 Tab.
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. Ví dụ CTabFolder

CTabFolder là một tab folder tùy biến. Các CTabItem có thể áp dụng style SWT.CLOSE, Cho phép người dùng có thể đóng 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

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();
    }

}