<更新記録>
2007年 12月 27日
執筆

姉妹サイト検索 Web検索


TableLayout

TableLayout

TableLayoutクラスは、Tableウィジェットのレイアウトを行うクラスです。 とはいっても、行うのは、列の幅のレイアウトのみです。

レイアウトを設定せずに、TableにTableColumnオブジェクトを格納する場合、 その後でTableColumnのsetWidthメソッドを呼び出し、ピクセル単位で幅を指定しなければなりません。 しかしTableLayoutを使用すれば、幅を割合で指定することができます。

プログラムは、Tableウィジェットの説明で示したサンプルに、 TableLayoutを使って、列幅を割合で指定したものです。

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
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 TableLayoutingComposite extends Composite {
	String[][] persons = {
			{"Venjamin", "man", "20"},
			{"Bobby", "man", "19"},
			{"Sarry", "woman", "34"},
			{"Anna", "woman", "27"}
	};
	
	public TableLayoutingComposite(Composite parent) {
		super(parent, SWT.NONE);
		
		Table table = new Table(this, SWT.FULL_SELECTION);
		
		// TableLayoutの作成と設定
		TableLayout layout = new TableLayout();
		layout.addColumnData(new ColumnWeightData(50));
		layout.addColumnData(new ColumnWeightData(35));
		layout.addColumnData(new ColumnWeightData(15));
		table.setLayout(layout);
		
		// 各TableColumnの幅は指定しなくてよい
		TableColumn nameColumn = new TableColumn(table, SWT.LEFT);
		nameColumn.setText("name");
		TableColumn sexColumn = new TableColumn(table, SWT.CENTER);
		sexColumn.setText("sex");
		TableColumn ageColumn = new TableColumn(table, SWT.RIGHT);
		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();
	}
}


Powered by VeryEasyCMS