Tables

A table can be considered a two-dimensional grid composed of several cells. A possible use for tables is the storage of the results of database queries. In order to add data to a table, it is necessary to use a TableItem object. Each instance of a TableItem object is responsible for an entire row of a table. The values can be set using the setText and setImage methods that takes an integer as parameter to identify which column should be modified.

In a similar fashion to the TableItem object, the TableColumn object receives a table as a parameter in order to associate both objects. Each time a TableColumn object is associated, a new column is created in the corresponding table.

The following code snippet shows how to create a SWT table:

      ...
        Display display = new Display(); 
        Shell shell = new Shell(display); 
        
        shell.setSize(500,400); 
        shell.setText("Table Example"); 
        shell.setLayout(new FillLayout( )); 
        
        Table table = new Table(shell, SWT.BORDER); 
        TableColumn tc1 = new TableColumn(table, SWT.CENTER); 
        TableColumn tc2 = new TableColumn(table, SWT.CENTER); 
        TableColumn tc3 = new TableColumn(table, SWT.CENTER); 
        tc1.setText("Name"); 
        tc2.setText("Age"); 
        tc3.setText("Country"); 
        tc1.setWidth(80); 
        tc2.setWidth(40); 
        tc3.setWidth(80); 
        table.setHeaderVisible(true); 
        TableItem item1 = new TableItem(table,SWT.NONE); 
        item1.setText(new String[] {"Rodrigo","28","France"}); 
        TableItem item2 = new TableItem(table,SWT.NONE); 
        item2.setText(new String[] {"Vinicius","28","USA"}); 
        TableItem item3 = new TableItem(table,SWT.NONE); 
        item3.setText(new String[] {"Rafael","28","Brazil"}); 
        shell.open( ); 
        while(!shell.isDisposed()){ 
            if(!display.readAndDispatch( )) 
                display.sleep( ); 
        } 
        display.dispose();
       ...

The table GUI regarding the corresponding code is presented as follows:

Tables

Despite being possible to set attributes directly on a column, it is a good practice to make use of the TableLayout class. TableLayout provides the addColumnData method, which makes it easier to describe the appearance of each column in the table.

JFace is a UI toolkit with classes for handling common UI programming tasks. JFace is designed to work with SWT and is system dependent. Its most relevant features are actions and viewers. The action mechanism allows user commands to be defined independently from their exact whereabouts in the UI. Viewers are model-based adapters for certain SWT widgets, simplifying the presentation of application data structured as lists, tables or trees, which is used in our example. TableLayout is part of the org.eclipse.jface.viewers package. It can be imported in a similar fashion to the SWT, which is explained in the beginning of the previous chapter by going to the following website: http://wiki.eclipse.org/JFace

The following code snippet shows how to use a TableLayout object:

    ...
    TableLayout layout = new TableLayout(); 
    layout.addColumnData(new ColumnWeightData(40, 80, true)); 
    layout.addColumnData(new ColumnWeightData(40, 80, true)); 
    layout.addColumnData(new ColumnWeightData(40, 80, true)); 
    Table table = new Table(shell, SWT.SINGLE); 
    table.setLayout(layout); 
    TableColumn column1 = new TableColumn(table, SWT.CENTER); 
    TableColumn column2 = new TableColumn(table, SWT.CENTER); 
    TableColumn column3 = new TableColumn(table, SWT.CENTER); 
    TableItem item = new TableItem(table, SWT.NONE); 
    item.setText( new String[] { "column 1", 
                                  "column 2", 
                                  "column 3" } ); 
    item = new TableItem(table, SWT.NONE); 
    item.setText( new String[] { "a", "b", "c" } );
    ...

Each time addColumnData is created, it adds a new column to the table. The ColumnWeightData object takes as parameter the weight, minimum width, and whether it is resizeable or not. It is important to remember that it is necessary to instantiate the TableLayout object to handle the column characteristics and also the TableColumns themselves.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset