jface
provides MessageDialog class to display message dialogs to user. MessageDialog
class provides following constructor to instantiate.
MessageDialog(Shell
parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage,
int dialogImageType, String[] dialogButtonLabels, int defaultIndex)
Following
table summarizes the parameters.
Parameter
|
Description
|
parentShell
|
Parent
shell
|
dialogTitle
|
Title
of the message dialog
|
dialogTitleImage
|
Dialog
title Image
|
dialogMessage
|
Dialog
message
|
dialogImageType
|
It
can be one of the following values:
MessageDialog.NONE
for a dialog with no image
MessageDialog.ERROR
for a dialog with an error image
MessageDialog.INFORMATION
for a dialog with an information image
MessageDialog.QUESTION
for a dialog with a question image
MessageDialog.WARNING
for a dialog with a warning image
|
dialogButtonLabels
|
An
array of labels for the buttons in the button bar
|
defaultIndex
|
The
index in the button label array of the default button
|
Note
a. You must call the
open method on the MessageDialog to open, else it is not displayed on the screen.
Ex:
Image
IMAGE_APP_LOGO = new Image(null, "C:\\Users
\\Documents\\Study\\app_logo.png");
MessageDialog
dialog = new MessageDialog(Display.getDefault().getActiveShell(),
"Hello", IMAGE_APP_LOGO,"Hello Message",
MessageDialog.INFORMATION, buttons, 2);
Following
is the complete working application.
package test; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Test { public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); /* Open shell window */ shell.open(); String[] buttons = { "OK", "CANCEL", "NOT DECIDED" }; Image IMAGE_APP_LOGO = new Image(null, "C:\\Users\\Documents\\Study\\app_logo.png"); MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Hello", IMAGE_APP_LOGO, "Hello Message", MessageDialog.INFORMATION, buttons, 2); int result = dialog.open(); System.out.println("You clicked " + buttons[result]); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } /* Dispose the display */ display.dispose(); } }
MessageDialog class
provides following static helper methods to quickly open commonly used dialogs.
static
boolean openConfirm(Shell parent, String title, String message)
Convenience
method to open a simple confirm (OK/Cancel) dialog. Return true if the user
presses the OK button, false otherwise
static void
openError(Shell parent, String title, String message)
Convenience
method to open a standard error dialog.
static void
openInformation(Shell parent, String title, String message)
Convenience
method to open a standard information dialog.
static
boolean openQuestion(Shell parent, String title, String message)
Convenience
method to open a simple Yes/No question dialog.
static void
openWarning(Shell parent, String title, String message)
Convenience
method to open a standard warning dialog.
Ex:
MessageDialog.openConfirm(Display.getDefault().getActiveShell(),
"Confirm Title", "This is Confirmation Message");
MessageDialog.openError(Display.getDefault().getActiveShell(),
"Error Title", "This is Error Message");
MessageDialog.openInformation(Display.getDefault().getActiveShell(),
"Information Title","This is Information Message");
MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
"Question Title", "This is Question Message");
MessageDialog.openWarning(Display.getDefault().getActiveShell(),
"Warning Title", "This is Warning Message");
Following
is the complete working application.
import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Test { public static void main(String[] args) { System.out.println(System.getProperty("user.home")); /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); /* Open shell window */ shell.open(); MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Confirm Title", "This is Confirmation Message"); MessageDialog.openError(Display.getDefault().getActiveShell(), "Error Title", "This is Error Message"); MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Information Title", "This is Information Message"); MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "Question Title", "This is Question Message"); MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning Title", "This is Warning Message"); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment