S-JIS[2013-09-15] 変更履歴

Eclipseプラグイン SWT タブ

Eclipseプラグイン開発で扱うSWTのタブについて。


概要

タブはTabFolderクラスを使う。


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
	TabFolder tab = new TabFolder(parent, SWT.NONE);
	tab.setLayoutData(new GridData(GridData.FILL_BOTH));

	{ // 1つめのタブ
		TabItem item = new TabItem(tab, SWT.NONE);
		item.setText("タブ1");

		Composite composite = new Composite(tab, SWT.NONE);
		composite.setLayout(new GridLayout());
		item.setControl(composite);

		Label label = new Label(composite, SWT.NONE);
		label.setText("ラベル1");
	}
	{ // 2つめのタブ
		TabItem item = new TabItem(tab, SWT.NONE);
		item.setText("タブ2");

		Composite composite = new Composite(tab, SWT.NONE);
		composite.setLayout(new GridLayout());
		item.setControl(composite);

		Label label = new Label(composite, SWT.NONE);
		label.setText("ラベル2");
	}

タブページ全体はTabFolderで作る。

各タブはTabItemで作る。

タブページの内容は、別途Compositeを作って実現する。Compositeにコンポーネント(LabelやText等)を配置していく。
Compositeの親はTabFolderになる。
TabItem#setControl()でCompositeを登録する。


タブの選択の検知

タブが選択されたときのイベントを取得する方法。

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
	TabFolder tab = 〜;
	tab.addSelectionListener(new SelectionListener() {

		@Override
		public void widgetSelected(SelectionEvent e) {
			TabFolder tab = (TabFolder) e.getSource();
			System.out.println(tab.getSelectionIndex());
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			widgetSelected(e);
		}
	});

SWTへ戻る / Eclipseプラグインへ戻る / Eclipseへ戻る / 技術メモへ戻る
メールの送信先:ひしだま