Tuesday, 13 December 2016

SWT: TabFolder widget

By using TabFolder class, you can define TabFolder widget. Only one of the styles TOP and BOTTOM may be specified.

Ex:
TabFolder employeeInfoTab = new TabFolder(shell, SWT.NONE);
employeeInfoTab.setBounds(10, 10, tabWidth, tabHeight);

You can create a tab items using TabItem class. Suppose, I want to add two tabs like "Personal Information", "Professional Information" to employeeInfoTab.

TabItem personalInfo = new TabItem(employeeInfoTab, SWT.NONE);
personalInfo.setText("Personal Information");

TabItem professionalDetails = new TabItem(employeeInfoTab, SWT.NONE);
professionalDetails.setText("Professional Information");

You ca add any widgets to the tab items using ‘setControl’ method.

personalInfo.setControl(personalnfoTable);
professionalDetails.setControl(professionalInfoTable);

Ideal way is add all the widgets to a composite and add the composite to the tab item.

Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Test {

 private static int shellWidth = 1000;
 private static int shellHeight = 700;

 private static int tableWidth = 800;
 private static int tableHeight = 600;

 private static int tabWidth = 800;
 private static int tabHeight = 600;

 private static int columnWidth = 400;

 private static void addPersonalDetails(Table personalnfoTable) {
  TableColumn id = new TableColumn(personalnfoTable, SWT.LEFT);
  id.setText("Name");
  id.setWidth(columnWidth);

  TableColumn name = new TableColumn(personalnfoTable, SWT.LEFT);
  name.setText("Value");
  name.setWidth(columnWidth);

  TableItem item1 = new TableItem(personalnfoTable, SWT.NONE);
  item1.setText(new String[] { "Name", "Hari Krishna" });

  TableItem item2 = new TableItem(personalnfoTable, SWT.NONE);
  item2.setText(new String[] { "Age", "26" });

  TableItem item3 = new TableItem(personalnfoTable, SWT.NONE);
  item3.setText(new String[] { "City", "Bangalore" });

 }

 private static void addProfessionalDetails(Table professionalInfoTable) {
  TableColumn id = new TableColumn(professionalInfoTable, SWT.LEFT);
  id.setText("Name");
  id.setWidth(columnWidth);

  TableColumn name = new TableColumn(professionalInfoTable, SWT.LEFT);
  name.setText("Value");
  name.setWidth(columnWidth);

  TableItem item1 = new TableItem(professionalInfoTable, SWT.NONE);
  item1.setText(new String[] { "Employee Id", "E529432" });

  TableItem item2 = new TableItem(professionalInfoTable, SWT.NONE);
  item2.setText(new String[] { "Designation", "Software Developer" });

  TableItem item3 = new TableItem(professionalInfoTable, SWT.NONE);
  item3.setText(new String[] { "Domain", "Security" });

  TableItem item4 = new TableItem(professionalInfoTable, SWT.NONE);
  item4.setText(new String[] { "Expertise In", "Java" });

 }

 private static void addWidgetsToShell(Display display, Shell shell) {

  TabFolder employeeInfoTab = new TabFolder(shell, SWT.NONE);
  employeeInfoTab.setBounds(10, 10, tabWidth, tabHeight);

  Table personalnfoTable = new Table(employeeInfoTab, SWT.FULL_SELECTION | SWT.BORDER);
  personalnfoTable.setBounds(100, 10, tableWidth, tableHeight);
  personalnfoTable.setLinesVisible(true);

  addPersonalDetails(personalnfoTable);

  Table professionalInfoTable = new Table(employeeInfoTab, SWT.FULL_SELECTION | SWT.BORDER);
  professionalInfoTable.setBounds(200, 10, tableWidth, tableHeight);
  professionalInfoTable.setLinesVisible(true);

  addProfessionalDetails(professionalInfoTable);

  TabItem personalInfo = new TabItem(employeeInfoTab, SWT.NONE);
  personalInfo.setText("Personal Information");
  personalInfo.setControl(personalnfoTable);

  TabItem professionalDetails = new TabItem(employeeInfoTab, SWT.NONE);
  professionalDetails.setText("Professional Information");
  professionalDetails.setControl(professionalInfoTable);

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment