Skip to content
Haixing Hu edited this page Sep 18, 2014 · 6 revisions

Dialog

This is the Dialog widget comes from the OPAL project, with some refactoring of the implementaion.

Usage

Formatting

You can format the message using some pseudo-HTML tags :

  • <br/> for adding a line break
  • <i>...</i> to render text in italic
  • <u>...</u> to render text in underline
  • <b>...</b> to render text in bold
  • <size>...</size> to increase/decrease text size. You can use the following syntaxes: <size=10> (10px), <size=+4>, <size=-4>
  • <color>...</color> to change foreground color. You can use the following syntaxes: <color=#FFCCAA> (HTML color code), <color=9,255,10> (RGB values) and <color=aliceblue> (HTML color code)
  • <backgroundcolor>...</backgroundcolor> to change background color. You can use the following syntaxes: <backgroundcolor=#FFCCAA> (HTML color code), <backgroundcolor=9,255,10> (RGB values) and <backgroundcolor=aliceblue> (HTML color code)

i18n

Common text in the dialog can be shown in any language by putting locale specific resource bundle named messages_<locale_id>.properties on the classpath and switching default locale. The library has following built-in bundles under ’src/main/resources’:

Bundle Locale
Default en
Simplified Chinese zh_CN

Display an error

The Dialog class provides two static methods :

public static void error(String title, String message);
public static void error(Shell shell, String title, String message);

where title is the title of the dialog box, and message the error message to display. If no shell is specified, the dialog box will be a on-top window.

Example:

Application Error Dialog

Dialog.error("CRASH AND BURN !", 
	"The application has performed an illegal action. "
  + "This action has been logged and reported.");

Display an information

The Dialog class provides two static methods :

public static void inform(String title, String text);
public static void inform(Shell shell, String title, String text);

where title is the title of the dialog box, and text is the text to display. If no shell is specified, the dialog box will be a on-top window.

Example:

Information Dialog

Dialog.inform("You've won!", "The game is over with the 15:3 score");

Display a confirmation

The Dialog class provides four static methods :

public static boolean isConfirmed(String title, String text);
public static boolean isConfirmed(Shell shell, String title, String text)
public static boolean isConfirmed(String title, String text, int timer)
public static boolean isConfirmed(Shell shell, String title, String text, int timer)

where title is the title of the dialog box, and text is the text to display. If no shell is specified, the dialog box will be a on-top window. If a timer is specified, this is the time in second before the "Yes" button is enabled.

Example:

Confirm Dialog

boolean confirm = Dialog.isConfirmed("Are you sure you want to quit?", 
									  "Please do not quit yet!");
System.out.println("Choice is..." + confirm);

Timed Confirm Dialog

boolean choice = Dialog.isConfirmed("Are you sure you want to quit?", 
									 "Please do not quit yet!", 10);
System.out.println("Choice is..." + choice);

Ask for a choice (radio button)

The Dialog class provides two static methods :

public static int radioChoice(String title, String text, int defaultSelection, 
							   String... values)
public static int radioChoice(Shell shell, String title, String text, 
							   int defaultSelection, String... values)

where title is the title of the dialog box, text is the text to display, defaultSelection is the index of the default selection (0-based index) and values is a list of propositions. If no shell is specified, the dialog box will be a on-top window.

Example:

Radio Choice Dialog

int choice = Dialog.radioChoice("You've got selection to make", 
			"Go ahead", 1, "Yes", "No", "May be");
System.out.println("Choice is..." + choice);

Ask for a choice (Windows vista style)

The Dialog class provides two static methods :

public static int choice(String title, String text, int defaultSelection,
 						 ChoiceItem... items);
public static int choice(Shell shell, String title, String text, 
						  int defaultSelection, ChoiceItem... items)

where title is the title of the dialog box, text is the text to display, defaultSelection is the index of the default selection (0-based index) and items is a list of propositions.

ChoiceItem is a POJO used to display the proposition, which is composed of a short text and a description.

If no shell is specified, the dialog box will be a on-top window.

Example:

Choice Dialog

int choice = Dialog.choice("What do you want to do with your "
							+ "game in\nprogress?", "", 1, 
  				new ChoiceItem("Exit and save my game", 
  					"Save your game in progress, then exit. " +
  					"This will\noverwrite any previously saved games."),
  				new ChoiceItem("Exit and don't save", 
  					"Exit without saving your game. " + 
  					"This is counted\nas a loss in your statistics."), 
  				new ChoiceItem("Don't exit", 
  					"Return to your game progress"));

System.out.println("Choice is..." + choice);

Display an Exception

The Dialog class provides a static method :

public static void showException(Throwable exception)

where exception is the exception to display.

Example:

Exception Dialog

Exception Dialog Hide Details

try {
  new BigDecimal("seven");
} catch (final Throwable e) {
  Dialog.showException(e);
}

Display input

The Dialog class provides two static methods :

public static String ask(String title, String text, String defaultValue)
public static String ask(Shell shell, String title, String text, String defaultValue)

where title is the title of the dialog box, text is the text to display and defaultValue is the default value of the input box. If no shell is specified, the dialog box will be a on-top window.

Example:

Input Dialog

String input = Dialog.ask("Enter you name", 
					"or any other text if you prefer", 
					"Bill Gates");
System.out.println("Choice is..." + input);

More complex stuffs

A dialog box is composed of two parts :

  • A message area
  • A footer area

Two Parts of a Dialog

Message Area

A message area is composed of 3 parts :

  • A title
  • An icon
  • A text
  • Eventually a progress bar

Each setter returns the message area object, so you can join statements.

Footer Area

A footer area is composed of different optional parts

  • A checkbox : use by the method addCheckbox(text, default selection)
  • A list of button labels : use the method setButtonLabels(String... labels)
  • A collapsable section
    • Use setExpanded(boolean expanded) to set the collapse/expanded state
    • Use setDetailText(String text) to set the text displayed when the panel is expanded
  • A footer
    • Use setFooterText(String text) to set the text displayed in the footer
    • Use setImage(Image image) to set the image displayed in the footer, on the left side

Each setter returns the footer area object, so you can join statements.

End

Once the message area and the footer area are built, use the method show() to open the dialog box. It returns the zero-based index of the selected button. You can also set the minimum height and width of the dialog box (by using the appropriate setters) or specifiy the buttons (by using a value of the enumeration Dialog.Type : CLOSE, NO_BUTTON, OK, OK_CANCEL, SELECT_CANCEL, SELECT_CANCEL, YES_NO).

Examples

Progressbar Dialog

Dialog dialog = new Dialog();
dialog.setTitle("Copying...");
dialog.setMinimumWidth(400);
dialog.getMessageArea()
	  .setTitle("Copying files") 
      .setIcon(Display.getCurrent().getSystemImage(SWT.ICON_INFORMATION))
      .setText("Location : from 'Others' to 'Others'<br/>" + 
               "File Name : <b>photo.jpg</b>")
      .addProgressBar(0, 100, 0);

A Complex Example of Dialog

Dialog dialog = new Dialog();
dialog.setTitle("Security Warning");
dialog.setMinimumWidth(400);
dialog.getMessageArea()
	  .setTitle("The publisher cannot be verified.\n" +
	  			"Do you want to run this software?") 
      .setIcon(Display.getCurrent().getSystemImage(SWT.ICON_WARNING))
      .setText("Name: C:\\Program Files\\eclipse\\eclipse.exe<br/>" +
               "Publisher: <b>Unknown Publisher</b><br/>" + 
               "Type: Application<br/>");

dialog.getFooterArea()
	  .addCheckBox("Always ask before opening this file", false)
	  .setButtonLabels("Run", "Cancel");
dialog.show();

Another Complex Example of Dialog

Another Complex Example of Dialog, Displaying the Details

Dialog dialog = new Dialog();
dialog.setTitle("Application Error");
dialog.getMessageArea()
	  .setTitle("CRASH AND BURN !")
      .setText("The application has performed an illegal action. " +
      			"This action has been logged and reported.")
      .setIcon(Display.getCurrent().getSystemImage(SWT.ICON_ERROR));
dialog.setButtonType(Dialog.Type.OK);
dialog.getFooterArea()
      .setExpanded(false)
      .addCheckBox("Don't show me this error next time", true)
      .setDetailText("More explanations to come...");
dialog.getFooterArea()
	  .setFooterText("Your application crashed because a developer " +
	  				  "forgot to write a unit test")
      .setIcon(SWTResourceManager.getImage(this.getClass(), 
      										"images/warning.png"));
dialog.show();

Example

An example is located in the source repository:

src/test/java/com/github/haixing_hu/swt/window/DialogExample.java