Dialog boxes are windows that are separated from the main application. It is usually placed in front of the user to catch his or her attention to an operation that needs to be performed. The dialog abstract class provides the basis for dialogs in SWT. It is possible to extend it in order to create a custom dialog or make use of the several templates already available, such as a confirmation message box or a file dialog.
The MessageBox
dialog is typically used to display information to the user, such as a possible error, or to obtain input from the user on an operation that needs to be performed. The dialog can be displayed with several buttons, such as yes or no. When the user makes a selection, the corresponding code branch is executed.
The trivial thing to do when using the MessageBox
dialog is to set its message through the setMessage
method. Nonetheless, there is a variety of icon styles along with several types of buttons that can be specified. We show the possible styles combinations, and afterwards, the description of the available icons, as follows:
SWT.OK
SWT.OK | SWT.CANCEL
SWT.YES | SWT.NO
SWT.YES | SWT.NO | SWT.CANCEL
SWT.RETRY | SWT.CANCEL
SWT.ABORT | SWT.RETRY | SWT.IGNORE
SWT.ERROR_ICON
: It is used to inform that an error has occurredSWT.ICON_INFORMATION
: It is used to give non-interactive informationSWT.ICON_QUESTION
: It is used for choosing an answer for the given questionSWT.ICON_WARNING
: It is used to inform the user about a potentially harmful actionSWT.ICON_WORKING
: It tells the user that the work is in progressThe following code snippet shows the use of the MessageBox
dialog:
... MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO); messageBox.setMessage("Do you really want to quit?"); messageBox.setText("Exiting Application"); int response = messageBox.open( ); if (response==SWT.YES) { shell.dispose(); System.exit(0); } ...
The following screenshot shows the result of the MessageBox
example:
The FileDialog
opens a dialog with buttons and a list box or tree view that enables the navigation through the filesystem. The programmer only needs to instantiate this class, specifying a few parameters, and does not need to perform any heavy programming.
... FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Open"); fd.setFilterPath("/"); String[] filterExt = {"*.txt"}; fd.setFilterExtensions(filterExt); String selected = fd.open( ); System.out.println(selected); ...
The corresponding GUI dialog is shown in the following screenshot:
The ColorDialog
has the objective of allowing the selection of a color in a widget or text. It is possible to select the desired color graphically, which can then be passed to a widget. It is also possible to select the RGB values manually.
... Text text = new Text(shell, SWT.CENTER | SWT.BORDER); text.setSize(200,25); ColorDialog cd = new ColorDialog(shell); cd.setText("ColorDialog"); cd.setRGB(new RGB(255,255,255)); RGB newColor = cd.open( ); text.setBackground(new Color(display, newColor)); ...
The corresponding widget can be seen as follows. Note that this widget is dependent on the operating system and may vary among them.
The FontDialog
can be used for selecting fonts for a text widget in a graphical user interface. Its use is similar to the aforementioned examples.
A snippet of code along with the corresponding output is shown as follows:
... Text text = new Text(shell, SWT.CENTER | SWT.BORDER); FontDialog fd = new FontDialog(shell, SWT.NONE); fd.setText("Font Selection"); fd.setRGB(new RGB(0,0,255)); FontData defaultFont = new FontData("Verdana",10,SWT.BOLD); fd.setFontData(defaultFont); FontData newFont = fd.open( ); text.setFont(new Font(display, newFont)); text.setForeground(new Color(display, fd.getRGB())); ...
The PrintDialog
allows the user to select a printer device to which an application can direct its output. In order to use it, the programmer must follow the same process as previously explained. We present a snippet of code regarding the use of PrintDialog:
... PrintDialog printDialog = new PrintDialog(shell, SWT.NONE); printDialog.setText("Print"); PrinterData printerData = printDialog.open(); Printer p = new Printer(printerData); p.startJob("PrintJob"); p.startPage(); Rectangle trim = p.computeTrim(0, 0, 0, 0); Point dpi = p.getDPI(); int leftMargin = dpi.x + trim.x; int topMargin = dpi.y / 2 + trim.y; GC gc = new GC(p); Font font = gc.getFont(); String printText = "printExample"; Point extent = gc.stringExtent(printText); gc.drawString(printText, leftMargin, topMargin + font.getFontData()[0].getHeight()); p.endPage(); p.endJob(); p.dispose(); ...
If this code is executed, the system configuration printer dialog is presented to the user, and it will print a simple sentence. In this example, the margins are set and a string is drawn using a specified font.