代表的なSWTコモンダイアログの使い方メモ
それぞれのクラスの言語ヘルプは、こちらのEclipse言語リファレンスに掲載されている。
ファイル・ダイアログ (FileDialog)
赤で着色した部分が、ファイル・ダイアログの表示初期化部分。青で着色した部分が、ダイアログの表示と選択されたファイル名を受け取る部分。
・SWT.SAVE : ファイルの保存ダイアログ
・SWT.OPEN : ファイルを開くダイアログ
・SWT.MULTI : 複数ファイルの選択可能なファイルを開くダイアログ
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
// レイアウト全体の基本設計
this.setSize(400, 100);
this.setText("ヴィジェットのテスト");
shell.setLayout(new RowLayout(SWT.VERTICAL));
// レイアウトに配置する要素(ヴィジェット)
Text t = new Text(shell, SWT.READ_ONLY);
t.setText("まだファイルが選択されていません");
// レイアウト下部に表示するボタン
Button button = new Button(shell, SWT.NULL);
button.setText("ダイアログの表示");
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// 読み込み用ダイアログを開く
FileDialog fileDlg = new FileDialog(shell,SWT.OPEN);
String [] exts = {"*.txt","*.*"};
fileDlg.setFilterExtensions(exts);
String [] filterNames = {"テキストファイル(*.txt)",
"全てのファイル(*.*)"};
fileDlg.setFilterNames(filterNames);
fileDlg.setText("ファイル・ダイアログのサンプル");
String selectedFilename = fileDlg.open();
if(selectedFilename != null && !selectedFilename.isEmpty()) t.setText(selectedFilename);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
ディレクトリ・ダイアログ (DirectoryDialog)
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
// レイアウト全体の基本設計
this.setSize(400, 100);
this.setText("ヴィジェットのテスト");
shell.setLayout(new RowLayout(SWT.VERTICAL));
// レイアウトに配置する要素(ヴィジェット)
Text t = new Text(shell, SWT.READ_ONLY);
t.setText("まだファイルが選択されていません");
// レイアウト下部に表示するボタン
Button button = new Button(shell, SWT.NULL);
button.setText("ダイアログの表示");
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// ディレクトリ選択ダイアログを開く
DirectoryDialog fileDlg = new DirectoryDialog(shell,SWT.NONE);
fileDlg.setText("ファイル・ダイアログのサンプル");
String selectedFilename = fileDlg.open();
if(selectedFilename != null && !selectedFilename.isEmpty()) t.setText(selectedFilename);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
カラー・ダイアログ (ColorDialog)
選択された色はRGB形式で返されます。
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
// レイアウト全体の基本設計
this.setSize(400, 100);
this.setText("ヴィジェットのテスト");
shell.setLayout(new RowLayout(SWT.VERTICAL));
// レイアウトに配置する要素(ヴィジェット)
Text t = new Text(shell, SWT.READ_ONLY);
t.setText("まだファイルが選択されていません");
// レイアウト下部に表示するボタン
Button button = new Button(shell, SWT.NULL);
button.setText("ダイアログの表示");
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// カラー選択ダイアログを開く
ColorDialog colorDlg = new ColorDialog(shell,SWT.NONE);
colorDlg.setText("カラー・ダイアログのサンプル");
RGB selectedColor = colorDlg.open();
if(selectedColor != null) t.setText(selectedColor.toString());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
フォント・ダイアログ (FontDialog)
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
// レイアウト全体の基本設計
this.setSize(400, 100);
this.setText("ヴィジェットのテスト");
shell.setLayout(new RowLayout(SWT.VERTICAL));
// レイアウトに配置する要素(ヴィジェット)
Text t = new Text(shell, SWT.READ_ONLY);
t.setText("まだファイルが選択されていません");
// レイアウト下部に表示するボタン
Button button = new Button(shell, SWT.NULL);
button.setText("ダイアログの表示");
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// フォント選択ダイアログを開く
FontDialog fontDlg = new FontDialog(shell,SWT.NONE);
fontDlg.setText("フォント・ダイアログのサンプル");
FontData selectedFont = fontDlg.open();
if(selectedFont != null) t.setText(selectedFont.toString());
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}



