SWT, Standard Widget Toolkit を利用した基本的なサンプルを紹介します。今回は List と Table クラスのウィジェットです。
List
List クラスのインスタンスはリスト(一次元のデータ列)を表示するウィジェットです。
List: SWTAppList.java
package list;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SWTAppList {
Shell shell;
String[] prefData = { "北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県",
"茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "山梨県", "長野県",
"新潟県", "富山県", "石川県", "福井県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県",
"京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県",
"山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県",
"大分県", "宮崎県", "鹿児島県", "沖縄県" };
public SWTAppList(Display display) {
shell = new Shell(display);
shell.setText("List");
initUI();
// shell.pack();
shell.setSize(100, 200);
shell.setLocation(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
shell.setLayout(new FillLayout());
List list = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
for (String prefName : prefData) {
list.add(prefName);
}
list.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
List l = (List) e.widget;
int idx = l.getSelectionIndex();
if (idx > -1) {
System.out.println(l.getItem(idx) + "がクリックされました。");
}
}
});
list.pack();
}
public static void main(String[] args) {
Display display = new Display();
new SWTAppList(display);
display.dispose();
}
}
実行例
List: コンソール上の出力
北海道がクリックされました。 東京都がクリックされました。
Table
Table および関連クラスのインスタンスは、二次元のテーブルにテキストやイメージのリストを(行単位で)表示するウィジェットです。
List: SWTAppTable.java
package table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
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 SWTAppTable {
Shell shell;
String[] colNames = { "チーム", "試合数", "勝数", "負数", "引分数", "勝率" };
String[][] rowData = { { "ソフトバンク", "24", "18", "4", "2", "0.818" },
{ "オリックス", "24", "15", "7", "2", "0.682" },
{ "日本ハム", "24", "16", "8", "0", "0.667" },
{ "中日", "24", "14", "10", "0", "0.583" },
{ "西武", "24", "12", "11", "1", "0.522" },
{ "ヤクルト", "24", "10", "12", "2", "0.455" },
{ "巨人", "24", "10", "13", "1", "0.435" },
{ "阪神", "24", "10", "14", "0", "0.417" },
{ "楽天", "24", "9", "13", "2", "0.409" },
{ "ロッテ", "24", "8", "14", "2", "0.364" },
{ "横浜", "24", "7", "13", "4", "0.35" },
{ "広島", "24", "6", "16", "2", "0.273" } };
public SWTAppTable(Display display) {
shell = new Shell(display);
shell.setText("Table");
initUI();
shell.pack();
shell.setLocation(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION
| SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i = 0; i < colNames.length; i++) {
int colAlign, colWidth;
if (i == 0) {
colAlign = SWT.LEFT;
colWidth = 150;
} else {
colAlign = SWT.RIGHT;
colWidth = 100;
}
TableColumn col = new TableColumn(table, colAlign);
col.setText(colNames[i]);
col.setWidth(colWidth);
}
for (String[] data : rowData) {
TableItem item = new TableItem(table, SWT.NULL);
item.setText(data);
}
table.pack();
}
public static void main(String[] args) {
Display display = new Display();
new SWTAppTable(display);
display.dispose();
}
}




0 件のコメント:
コメントを投稿