Monday, 12 December 2016

SWT: Table widget

Table class is used to define table widget. You can apply following sty;es to table widget.
Style
Description
BORDER
Style constant for bordered behavior
SINGLE
Style constant for single selection behavior in lists and single line support on text fields
MULTI
Style constant for multi-selection behavior in lists and multiple line support on text fields
CHECK
Style constant for check box behavior
FULL_SELECTION
Style constant for full row selection behavior and selection constant indicating that a full line should be drawn.
HIDE_SELECTION
Style constant for selection hiding behavior when the widget loses focus
VIRTUAL
Style constant to allow virtual data
NO_SCROLL
Style constant for no scrollbar behavior. If you want scroll bar behavior, you can specify it by using the constants H_SCROLL or V_SCROLL .

Ex:
Table employee = new Table(shell, SWT.BORDER);
employee.setBounds(10, 10, tableWidth, tableHeight);
employee.setHeaderVisible(true);
employee.setLinesVisible(true);

How to add columns to a table?
By using TableColumn widget, you can add columns to a table.

TableColumn id = new TableColumn(employee, SWT.LEFT);
id.setText("Id");
id.setWidth(columnWidth);

You can apply any one of the following styles to table column.
Style
Description
LEFT
Style constant for align left behavior
RIGHT
Style constant for align right behavior
CENTER
Style constant for align center behavior

How to add content to a table?
You can add content to a table using TableItem widget.

TableItem emp1 = new TableItem(employee,SWT.NONE);
emp1.setText(new String[] {"1","Hari Krishna","26", "Bangalore"});

You can also use other form of setText method ‘setText(int, string)’, to set a value at particular field.
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.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 columnWidth = 200;

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

  Table employee = new Table(shell, SWT.FULL_SELECTION | SWT.CHECK | SWT.BORDER);
  employee.setBounds(100, 10, tableWidth, tableHeight);
  employee.setHeaderVisible(true);
  employee.setLinesVisible(true);

  TableColumn id = new TableColumn(employee, SWT.LEFT);
  id.setText("Id");
  id.setWidth(columnWidth);

  TableColumn name = new TableColumn(employee, SWT.LEFT);
  name.setText("Name");
  name.setWidth(columnWidth);

  TableColumn age = new TableColumn(employee, SWT.LEFT);
  age.setText("Age");
  age.setWidth(columnWidth);

  TableColumn city = new TableColumn(employee, SWT.LEFT);
  city.setText("City");
  city.setWidth(columnWidth);

  TableItem emp1 = new TableItem(employee, SWT.NONE);
  emp1.setText(new String[] { "1", "Hari Krishna", "26", "Bangalore" });
  emp1.setChecked(true);

  TableItem emp2 = new TableItem(employee, SWT.NONE);
  emp2.setText(new String[] { "2", "Ritweek", "29", "Bangalore" });

  TableItem emp3 = new TableItem(employee, SWT.NONE);
  emp3.setText(new String[] { "3", "Kiran", "28", "Bangalore" });
 }

 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