<更新記録>
2007年 12月 28日
執筆

姉妹サイト検索 Web検索


MessageDialog

MessageDialog

MessageDialog(
	Shell parentshell,
	String dialogTitle, 
	Image dialogTitleImage, 
	String dialogMessage, 
	int dialogImageType,
	String[] dialogButtonLabels,
	int defaultIndex
);

dialogTitleImage引数では、ダイアログのタイトルバーに表示する画像を指定します。

dialogImageType引数には、MessageDialogクラスの定数の中から1つを指定することができます。 MessageDialog.ERROR、MessageDialog.INFORMATION、MessageDialog.NONE、 MessageDialog.QUESTION、MessageDialog.WARNINGのいずれかを指定することができます。 指定された画像は、ダイアログの左側に表示されます。

dialogButtonLabels引数には、ダイアログに表示したいボタンのラベルの文字列配列を指定します。

defaultIndex引数には、ダイアログを開いたときに、どのボタンがフォーカスを得ているかを指定します。 0番目ならdialogButtonLabels[0]のラベルのボタンにデフォルトでフォーカスがあります。

MessageDialogオブジェクトを作成したら、次にopenメソッドを呼び出し、実際にダイアログを表示します。 ユーザが何れかのボタンを押す等してダイアログが消えたら、戻り値に数値が返ります。 0が返った場合、コンストラクタで指定したdialogButtonLabels[0]のラベルのボタンが選択されたことを意味します。 ボタンを選択した以外の手段でダイアログが終了した場合は、-1が返ります。

MessageDialogDemonstrate.java
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class MessageDialogDemonstrate extends ApplicationWindow {

	private MessageDialogDemonstrate() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		
		Button button = new Button(parent, SWT.PUSH);
		button.setText("open dialog");
		button.pack();
		
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				MessageDialog msgDlg = new MessageDialog(
						getShell(),
						"ダイアログのテスト",
						null,
						"きちんとダイアログは表示されたでしょうか・・",
						MessageDialog.ERROR,
						new String[] {"表示された", 
							"表示されなかった", 
							"どちらでもない"
						},
						0);
				int dlgReturnValue = msgDlg.open();
			}
		});
		
		parent.pack();
		return parent;
	}
	
	public static void main(String[] args) {
		Window w = new MessageDialogDemonstrate();
		w.setBlockOnOpen(true);
		w.open();
		Display.getCurrent().dispose();
	}
}


Powered by VeryEasyCMS