Tableクラスは、2次元のデータ(つまるところの表)を表すためのウィジェットです。
テーブルを作成するには、Tableクラス、TableItemクラス、 TableColumnクラスを使用します。
Tableクラスはテーブルそのものを、TableItemクラスは横(行)を、TableColumnクラスは縦(列)を表します。
テーブル作成の手順は次のようになります。
Table(Composite parent, int style)Tableクラスに指定できるスタイルには、SWT.SINGLE、SWT.MULTI、SWT.CHECK、SWT.FULL_SELECTION、SWT.HIDE_SELECTION、SET.VIRTUALがあります。 SWT.SINGLE及びSWT.MULTIは、アイテムを同時に1つ選択できるか複数選択できるかを表します。 SWT.FULL_SELECTIONは、アイテムを選択したときに、すべての列にわたって選択した状態になることを表します。
TableColumn(Table parent, int style) TableColumn(Table parent, int style, int index)TableColumnクラスは、テーブルの列を表します。 もしも3列ほしい場合は、TableColumnオブジェクトを3つ作成します。 setWidthメソッドで列の幅を指定し、setTextメソッドで列名を指定します。
TableItem(Table parent, int style) TableItem(Table parent, int style, int index)TableItemクラスは、テーブルの行(のデータ)を表します。 setText(String[])メソッドでは、複数の列のテキストを一度に設定できます。 setText(int, String)メソッドでは、指定したインデックスの列のテキストを設定できます。

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
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 TableComposite extends Composite {
String[][] persons = {
{"Venjamin", "man", "20"},
{"Bobby", "man", "19"},
{"Sarry", "woman", "34"},
{"Anna", "woman", "27"}
};
public TableComposite(Composite parent) {
super(parent, SWT.NONE);
Table table = new Table(this, SWT.FULL_SELECTION);
TableColumn nameColumn = new TableColumn(table, SWT.LEFT);
nameColumn.setWidth(150);
nameColumn.setText("name");
TableColumn sexColumn = new TableColumn(table, SWT.CENTER);
sexColumn.setWidth(100);
sexColumn.setText("sex");
TableColumn ageColumn = new TableColumn(table, SWT.RIGHT);
ageColumn.setWidth(50);
ageColumn.setText("age");
for (byte i=0 ; i < persons.length ; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(persons[i]);
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setSize(300, 300);
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
TableComposite td = new TableComposite(shell);
td.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
table.setHeaderVisible(true);ここでは、各列で設定した名前を、一番上に表示するかどうかを指定しています。 これを指定しないと、一番上に、"name" "sex" "age"のラベルが表示されません。
table.setLinesVisible(true);ここでは、各行間に線を引くかを指定しています。