ダイアログについて

メッセージダイアログ MessageBox

MessageBoxは何らかのメッセージを表示するためのダイアログです。openメソッドを実行するとダイアログが表示され、戻り値によりどのボタンが押下されたか分かります。
  MessageBoxeには、次に示す相互に排他的な5つのスタイルのうち、どれか1つのスタイルを定義する必要があります。

  • ICON_ERRORは、エラー・メッセージを表示します。
  • ICON_INFORMATIONは、情報メッセージを表示します。
  • ICON_QUESTIONは、質問メッセージを表示します。
  • ICON_WARNINGは、警告メッセージを表示します。
  • ICON_WORKINGは、作業メッセージを表示します。

この他にも、MessageBoxeは次のように幾つかのスタイルをサポートしています。それぞれのスタイルが、選択用のボタンを表します。

  • OK, OK | CANCEL
  • YES | NO, YES | NO | CANCEL
  • RETRY | CANCEL
  • ABORT | RETRY | IGNORE

ボタンの組み合わせ

MessageBoxコンストラクタのスタイルを指定することで、以下のようにボタンの組み合わせを指定することができます。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public class Main {

protected Shell shell;
private Button bNone;
private Button bError;
private Button bInfo;
private Button bQuestion;
private Button bWarning;

/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
    try {
        Main window = new Main();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
/**
 * Open the window.
 */
public void open() {
    //初期化
    Display display = Display.getDefault();
    shell = new Shell();
    shell.setText("MessageBoxTest");
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    createRadioButtons();

    createButton("Open MessageBox - OK", SWT.OK);
    createButton("Open MessageBox - OK | CANCEL", SWT.OK | SWT.CANCEL);
    createButton("Open MessageBox - YES | NO", SWT.YES | SWT.NO);
    createButton("Open MessageBox - YES | NO | CANCEL", SWT.YES | SWT.NO | SWT.CANCEL);
    createButton("Open MessageBox - RETRY | CANCEL", SWT.RETRY | SWT.CANCEL);
    createButton("Open MessageBox - ABORT | RETRY | IGNORE", SWT.ABORT | SWT.RETRY | SWT.IGNORE);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private void createRadioButtons() {
    Composite c = new Composite(shell, SWT.NONE);
    c.setLayout(new GridLayout(5, false));

    bNone = new Button(c, SWT.RADIO);
    bNone.setText("No Icon");
    bNone.setSelection(true);

    bError = new Button(c, SWT.RADIO);
    bError.setText("ICON_ERROR");

    bInfo = new Button(c, SWT.RADIO);
    bInfo.setText("ICON_INFORMATION");

    bQuestion = new Button(c, SWT.RADIO);
    bQuestion.setText("ICON_QUESTION");

    bWarning = new Button(c, SWT.RADIO);
    bWarning.setText("ICON_WARNING");

}
private void createButton(String text, int style){
    Button b = new Button(shell, SWT.PUSH);
    b.setText(text);
    b.addSelectionListener(new ButtonSelectionHandler(style));
}

class ButtonSelectionHandler extends SelectionAdapter{
    private int style;
    public ButtonSelectionHandler(int style){
        this.style = style;
    }
    public void widgetSelected(SelectionEvent e) {

        int iconType = getIconType();

        MessageBox box = new MessageBox(shell, style | iconType);
        box.setText("タイトルをここに");
        box.setMessage("メッセージをここに");
        int ret = box.open();
        switch(ret){
            case SWT.OK:
                System.out.println("OK is pressed.");
                break;
            case SWT.YES:
                System.out.println("YES is pressed.");
                break;
            case SWT.NO:
                System.out.println("NO is pressed.");
                break;
            case SWT.IGNORE:
                System.out.println("IGNORE is pressed.");
                break;
            case SWT.CANCEL:
                System.out.println("Cancel is pressed.");
                break;
            case SWT.ABORT:
                System.out.println("Aboart is pressed.");
                break;
            case SWT.RETRY:
                System.out.println("Retry is pressed.");
                break;
        }
    }

    private int getIconType() {
        if (bNone.getSelection()){
            return 0;
        }
        if (bInfo.getSelection()){
            return SWT.ICON_INFORMATION;
        }
        if (bWarning.getSelection()){
            return SWT.ICON_WARNING;
        }
        if (bError.getSelection()){
            return SWT.ICON_ERROR;
        }
        if (bQuestion.getSelection()){
            return SWT.ICON_WARNING;
        }
        return 0;
    }
}

アイコンの表示

openメソッドが返す値

openメソッドが返す値は以下に示す7つがあります。この値を調べることで、どのボタンが押下されたか調べることができます。

  • SWT.OK
  • SWT.CANCEL
  • SWT.YES
  • SWT.NO
  • SWT.IGNORE
  • SWT.ABORT
  • SWT.RETRY

ファイル選択ダイアログ FileDialog

FileDialogはファイルを選択するためのダイアログです。
  コンストラクタ引き数にスタイルを指定することで、さまざまなダイアログを作成することができます。   SWT.OPENかSWT.SAVEを指定することで読み込み用と保存用のダイアログが表示されます。指定による違いはダイアログのタイトルだけで、基本時には同じものです。
  openメソッドを実行するとダイアログが表示され、戻り値として選択されたファイルの絶対パスが返ります。キャンセルが押されるとnullが返されます。

読み込み用ダイアログの表示

1
2
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
String file = dialog.open();

保存用ダイアログの表示

1
2
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
String file = dialog.open();

ファイルフィルターを使ったダイアログの作成   拡張子のString配列を設定することで、ファイルフィルターを作れます。フィルターの名前も設定したフィルターに対応するように作成します。

1
2
3
4
5
6
7
8
9
//スタイルを省略した場合、SWT.OPENがデフォルト
FileDialog dialog = new FileDialog(shell);
String [] exts = {"*.java","*.txt","*.html;*.htm"};
String [] filterNames = {"Javaファイル(*.java)", 
                         "テキストファイル(*.txt)",
                         "HTMLファイル(*.html, *.htm)"};
dialog.setFilterExtensions(exts);
dialog.setFilterNames(filterNames);
String file = dialog.open();

複数選択可能なファイルダイアログの表示
  FileDialogのコンストラクタにMULTIを指定すると、複数のファイルを選択できるようになります。
  この場合、getFileNamesメソッドで複数選択されたファイルを取得します。しかしここで戻ってくる値は、ファイル名しか与えられないため、別途getFilterPathでディレクトリ名を取得する必要があるでしょう。またsetFilterPathメソッドで初期ディレクトリを指定できます。

1
2
3
4
5
6
FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
String path = "c:\\";
dialog.setFilterPath(path);
String file = dialog.open();
String [] files = dialog.getFileNames();
String path = dialog.getFilterPath();

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public void widgetSelected(SelectionEvent e) {
      if (e.getSource() == button1){
            FileDialog dialog = new FileDialog(shell, SWT.OPEN);
            String file = dialog.open();
            System.out.println("Selected file: " + file);
      }
      if (e.getSource() == button2){
            FileDialog dialog = new FileDialog(shell, SWT.SAVE);
            String file = dialog.open();
            System.out.println("Selected file: " + file);
      }
      if (e.getSource() == button3){
            //スタイルを省略した場合、SWT.OPENがデフォルト
            FileDialog dialog = new FileDialog(shell);
            String [] exts = {"*.java","*.txt","*.html;*.htm"};
            String [] filterNames = {"Javaファイル(*.java)", 
                                     "テキストファイル(*.txt)",
                                     "HTMLファイル(*.html, *.htm)"};
            dialog.setFilterExtensions(exts);
            dialog.setFilterNames(filterNames);
            String file = dialog.open();
            System.out.println("Selected file: " + file);
       }
       if (e.getSource() == button4){
            FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
            String path = "c:\\";
            dialog.setFilterPath(path);
            String file = dialog.open();
            System.out.println("Selected file: " + file);

            if (file == null){
                    return;
            }

            String [] files = dialog.getFileNames();
            path = dialog.getFilterPath();
            for (int i=0; i<files.length; i++){
                    System.out.println("[" + i + "] " + path + "\\" + files[i]);
            }
       }
}

ディレクトリ選択ダイアログ DirectoryDialog

ディレクトリを選択するためのダイアログです。
  これまで出てきたダイアログと同様にopenメソッドを実行するとダイアログが表示され、戻り値として選択されたディレクトリの文字列が返ります。
  キャンセルが押下された場合、nullが返ります。またsetFilterPathメソッドで初期ディレクトリ、setMessageメソッドでダイアログに表示されるメッセージを指定可能です。
  DirectoryDialogのコンストラクタDirectoryDialog(Shell parent, int style)でスタイルも指定できますが、ここで指定されたスタイルはスーパークラスであるDialogに適応されます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Button button = new Button(shell, SWT.PUSH);
button.setText("Open Directory Dialog");
button.addSelectionListener(new SelectionAdapter(){
     public void widgetSelected(SelectionEvent e) {
            //コンストラクタで指定されるスタイルは、Dialogクラスに適応される。
            DirectoryDialog dialog = new DirectoryDialog(shell);
            dialog.setFilterPath("c:\\");
            dialog.setMessage("ここにメッセージを設定できます。");
            String dir = dialog.open();
            if (dir == null){
                    return;
            }
            System.out.println("Selected Directory: " + dir);
     }
});

色選択ダイアログ ColorDialog

ColorDialogは色を選択するためのダイアログです。
  ColorDialogのopenメソッドを実行すると、下のようなダイアログが表示され、戻り値としてRGBが返ります。
  キャンセルボタンが押された場合、nullが返されます。下のサンプルプログラムは、ColorDialogでLabelの背景色を変えることができます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
final Label label = new Label(shell, SWT.BORDER);

Button b = new Button(shell, SWT.PUSH);
b.setText("Open Color Dialog");

b.addSelectionListener(new SelectionAdapter(){
        public void widgetSelected(SelectionEvent e) {
               ColorDialog dialog = new ColorDialog(shell);
               RGB rgb = dialog.open();
               if (rgb == null){
                      return;
               }
               System.out.println("Selected Color: " + rgb);
               Color color = new Color(shell.getDisplay(), rgb);
               label.setBackground(color);
               color.dispose();
        }
});

フォント選択ダイアログ FontDialog

FontDialogはフォントを選択するためのダイアログです。
  他のダイアログと同様にopenメソッド実行後ダイアログが表示され、戻り値としてFontDataを得られます。またキャンセルボタンが押されるとnullが返ります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        GridLayout gl = new GridLayout(1, false);
        shell.setLayout(gl);

        final Label label = new Label(shell, SWT.BORDER);
        label.setText("Test String");
        GridData gd = new GridData();
        gd.widthHint = 200;
        gd.heightHint = 100;
        label.setLayoutData(gd);

        Button b = new Button(shell, SWT.PUSH);
        b.setText("Open Font Dialog");

        b.addSelectionListener(new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                    FontDialog dialog = new FontDialog(shell);
                    FontData fd = dialog.open();
                    if (fd == null){
                        return;
                    }
                    Font font = new Font(shell.getDisplay(), fd);
                    label.setFont(font);
                }
        });