テーブル(Table)は2次元の表を表示することができます。ヘッダの設定にはTableColumn、データの設定にはTableItemを使用します。SwingのJTableと比較するとシンプルですが、機能は貧弱なように見受けられます。
import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class TableSample1 { public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell(display); shell.setText("Table Sample1"); shell.setLayout(new FillLayout()); Table table = new Table(shell,SWT.MULTI|SWT.FULL_SELECTION|SWT.BORDER); // ヘッダを可視にする table.setHeaderVisible(true); // ヘッダを設定 String[] cols = {"列1","列2","列3"}; for(int i=0;i<cols.length;i++){ TableColumn col = new TableColumn(table,SWT.LEFT); col.setText(cols[i]); col.setWidth(50); } // データを設定 for(int i=0;i<5;i++){ TableItem item = new TableItem(table,SWT.NULL); String[] data = {String.valueOf(i+1) + "-1", String.valueOf(i+1) + "-2", String.valueOf(i+1) + "-3"}; item.setText(data); } shell.pack(); shell.open(); while (!shell.isDisposed ()){ if (!display.readAndDispatch ()){ display.sleep (); } } display.dispose (); } }
以下にTableとTableItemの代表的なメソッドの使用例を示します。
// セルの分割線を表示 table.setLinesVisible(true); // 要素数を取得 int itemCount = table.getItemCount(); // 全ての要素を一度に配列で取得することもできます TableItem[] items = table.getItems(); // 選択されている要素数を取得 int selectedCount = tree.getSelectionCount(); // 指定したインデックスの要素が選択されているか調べる boolean select = table.isSelected(0); // 選択されている要素のインデックスを全て取得する int[] selectedIndex = table.getSelectionIndices(); // 選択されている要素を取得する TableItem[] selectedItems = tree.getSelection(); // 指定したインデックスの要素を削除 table.remove(0); // 0,1,2番目の要素を削除 int delIndex = {0,1,2}; table.remove(delIndex); // 0〜2までの要素を削除 table.remove(0,2); // 全ての要素を削除 table.removeAll();
最終更新時間:2004年03月09日 03時40分57秒