今回は Confirm Dialog。 Confirm Dialog はユーザーに「はい」、「いいえ」などを選択させるダイアログです。
Confirm Dialog
Confirm Dialog を表示させるには javax.swing.JOptionPane の static メソッド showConfirmDialog() メソッドを使用します。 このメソッドのシグニチャはこんな感じ:
static int showConfirmDialog(Component parent, Object message) static int showConfirmDialog(Component parent, Object message, String title, int optionType) static int showConfirmDialog(Component parent, Object message, String title, int optionType, int messageType) static int showConfirmDialog(Component parent, Object message, String title, int optionType, int messageType, Icon icon)
引数をもう少し詳しくまとめると
引数 | 型 | 必須 | デフォルト値 | 説明 |
---|---|---|---|---|
parent | java.awt.Component | Yes | - | 親となる GUI コンポーネント |
message | Object | Yes | - | メッセージ |
title | String | No | "オプションの選択" | ダイアログのタイトル |
optionType | int | No | JOptionPane. YES_NO_CANCEL_OPTION |
オプション・タイプ |
messageType | int | No | JOptionPane. QUESTION_MESAGE |
メッセージ・タイプ |
icon | javax.swing.Icon | No | アイコン |
メッセージ・タイプ (messageType) に関しては前回を参照。
オプション・タイプ
「オプション・タイプ」はユーザーに提示する選択肢です。 Confirm Dialog で指定できるのは以下の4つ:
- DEFAULT_OPTION
- YES_NO_OPTION
- YES_NO_CANCEL_OPTION
- OK_CANCEL_OPTION
それぞれのオプション・タイプを指定し場合に返される可能性のある値は下表の通り:
オプション・タイプ | 返り値 |
---|---|
DEFAULT_OPTION | OK_OPTION CLOSED_OPTION |
YES_NO_OPTION | YES_OPTION NO_OPTION CLOSED_OPTION |
YES_NO_CANCEL_OPTION | YES_OPTION NO_OPTION CANCEL_OPTION CLOSED_OPTION |
OK_CANCEL_OPTION | OK_OPTION CANCEL_OPTION CLOSED_OPTION |
ダイアログを「×」閉じた場合に「CLOSE_OPTION」が返されるのが要注意。 通常は「NO_OPTION」、「CANCEL_OPTION」などと同じ処理をすることになるかと。 それにしても、オプション・タイプと返り値が、どちらも int 型で名前も XXX_OPTION なので分かりづらいですねぇ。
サンプルコード
それではサンプルコード。
単純な Confirm Dialog
ダイアログの表示自体は簡単です。 その後、ユーザーが選択した値を受けて適切な処理を行います:
import javax.swing.JFrame; import javax.swing.JOptionPane; import static javax.swing.JOptionPane.*; JFrame parent = ...; int result // 下図 上 result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message."); // 下図 下 result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message.", "Confirm Dialog", YES_NO_OPTION); switch(result){ case YES_OPTION: println "YES"; break; case NO_OPTION: println "NO"; break; case CLOSED_OPTION: println "CLOSED"; break; default : println "Unknown value : "+result; }

同様のダイアログを Groovy + SwingBuilder で表示するにはこんな感じ(上図 上):
import groovy.swing.SwingBuilder import static javax.swing.JOptionPane.* JFrame parent = ... def pane = new SwingBuilder().optionPane( message:'Here is a confirmation message.', messageType:QUESTION_MESSAGE, optionType:YES_NO_CANCEL_OPTION) pane.createDialog(parent, 'オプションの選択').visible = true int result = pane.value
選択された値 (result) を取得するためは JOptionPane オブジェクトの value プロパティを読み取ります。
オプション・タイプあれこれ
オプション・タイプをあれこれ変更してダイアログを表示させてみましょう:
import javax.swing.JFrame; import javax.swing.JOptionPane; import static javax.swing.JOptionPane.*; JFrame parent = ...; int result; result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message.", "Default Option", DEFAULT_OPTION, QUESTION_MESSAGE); result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message.", "Yes/No Option", YES_NO_OPTION, QUESTION_MESSAGE); result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message.", "Yes/No/Cancel Option", YES_NO_CANCEL_OPTION, QUESTION_MESSAGE); result = JOptionPane.showConfirmDialog(parent, "Here is a confirmation message.", "OK/Cancel Option", OK_CANCEL_OPTION, QUESTION_MESSAGE);
表示結果はこんなんです:


Java Swing Hacks ―今日から使える驚きのGUIプログラミング集
- 作者: Joshua Marinacci,Chris Adamson,神戸博之(監訳),島田秋雄(監訳),加藤慶彦
- 出版社/メーカー: オライリージャパン
- 発売日: 2006/04/01
- メディア: 単行本(ソフトカバー)
- 購入: 7人 クリック: 100回
- この商品を含むブログ (38件) を見る