<更新記録>
2007年 12月 11日
作成
2008年 6月 3日
更新

姉妹サイト検索 Web検索


SashForm

便利なSashForm

こちら で作成したサンプルプログラムとほぼ同じものを、org.eclipse.swt.custom.SashFormクラスを用いて実装してみます。

SashFormクラスは、Compositeを継承しており、この中に複数のウィジェットを格納することができます。 そして、その格納したウィジェットごとの領域の割合を、 ウィジェットとウィジェットの間に自動生成されたSashウィジェットをドラッグすることによって、 自動的に領域の割合を計算・設定してくれます。 SashFormの中に格納したウィジェットの分割方向が縦方向なのか横方向なのかは、 コンストラクタに指定するスタイルで指定することができます。SWT.VERTICALが縦分割、SWT.HORIZONTALが横分割です。 最後に、分割の割合の初期設定をします。 これはSashForm#setWeightsで指定します。指定はintの配列で行い、その要素数はSashFormに格納されているウィジェットの数で、 各要素の合計は100です(つまりパーセント指定)。

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class Main extends ApplicationWindow {

	private Main() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		parent.setLayout(null);
		new SashFormComposite(parent);

		SashForm form = new SashForm(parent, SWT.HORIZONTAL);
		
		Button leftButton = new Button(form, SWT.PUSH);
		leftButton.setText("left");
		Button rightButton = new Button(form, SWT.PUSH);
		rightButton.setText("right");
		
		form.setWeights(new int[]{50, 50});
		
		form.setSize(100,100);
		parent.pack();
		return parent;
	}
	
	public static void main(String[] args) {
		Window w = new Main();
		w.setBlockOnOpen(true);
		w.open();
		Display.getCurrent().dispose();
	}
}

どうでしょうか。 ソースコードはかなり簡単になっています。 しかし機能は先ほど作成したサンプルプログラムと同等です。 格納されている特定のウィジェットを最大化するsetMaximizedControlメソッド)も用意されていて便利です。


Powered by VeryEasyCMS