Version 2.1
Copyright © 2010, 2011, 2012 Lars Vogel
14.01.2012
| Revision History | ||
|---|---|---|
| Revision 0.1 | 14.09.2010 | Lars Vogel |
| Created | ||
| Revision 0.2 - 2.1 | 29.11.2010 - 14.01.2012 | Lars Vogel |
| bug fixes and enhancements | ||
Table of Contents
Dialogs
allow to prompt the user for additional information or provide
the user
with feedback. The Eclipse platform offers several
different SWT and
JFace
Dialogs.
SWT offers an API for the use of the native dialogs from the OS platform. The SWT dialogs are the following:
ColorDialog - For selecting a color
DirectoryDialog - For selecting a directory
FileDialog - For selecting a file
FontDialog - For selecting a font
MessageBox - For opening a Message Dialog to the user
The following shows how to open the
MessageBox
dialog.
// Message with ok and cancel button and info icon MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL); dialog.setText("My info"); dialog.setMessage("Do you really want to do this?"); returnCode = dialog.open();
JFace contains additional
Dialogs
which are more flexible then the SWT
Dialogs
It
provides
static methods to open commonly used dialogs.
You can for example use the
MessageDialog
class which allows to customize the
Buttons
which should be used.
MessageDialog
provides also static methods to open commonly used
Dialogs, e.g. an info or a warning
Dialog.
// Customized MessageDialog with configured buttons MessageDialog dialog = new MessageDialog(shell, "My Title", null, "My message", MessageDialog.ERROR, new String[] { "First", "Second", "Third" }, 0); int result = dialog.open(); System.out.println(result); // A few standard message dialog MessageDialog.openConfirm(shell, "Confirm", "Please confirm"); MessageDialog.openError(shell, "Error", "Error occured"); MessageDialog.openInformation(shell, "Info", "Info for you"); MessageDialog.openQuestion(shell, "Question", "Really, really?"); MessageDialog.openWarning(shell, "Warning", "I warn you");
JFace also provides the
ErrorDialog
class
which allows to report a
Status
object.
Status
objects are frequently used in Eclipse to report
errors.
The
org.eclipse.jface.dialogs.Dialog
class can be extend to create own simple Dialogs. This class creates
a simple area in which you can place controls and add an OK and
Cancel button to it.
Your class needs to implement
have
createDialogArea
method. This methods gets an Composites which expects to get
a
GridData
object assigned as its layout data. Via the
super.createDialogArea(parent)
mehod call, you can get an
Composite
to which you can add your controls.
You can also define your own
Dialogs
based on the
TitleAreaDialog
class.
TitleAreaDialog
has a reserved space for providing feedback to the user. You can set
the text in this space via the
setMessage()
and
setErrorMessage()
methods.
The following shows an example for a custom defined
TitleAreaDialog
and shows how to open this dialog.
package de.vogella.rcp.intro.dialogs.custom.dialogs; import org.eclipse.jface.dialogs.IMessageProvider; import org.eclipse.jface.dialogs.TitleAreaDialog; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class MyDialog extends TitleAreaDialog { private Text firstNameText; private Text lastNameText; private String firstName; private String lastName; public MyDialog(Shell parentShell) { super(parentShell); } @Override public void create() { super.create(); // Set the title setTitle("This is my first own dialog"); // Set the message setMessage("This is a TitleAreaDialog", IMessageProvider.INFORMATION); } @Override protected Control createDialogArea(Composite parent) { GridLayout layout = new GridLayout(); layout.numColumns = 2; // layout.horizontalAlignment = GridData.FILL; parent.setLayout(layout); // The text fields will grow with the size of the dialog GridData gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = GridData.FILL; Label label1 = new Label(parent, SWT.NONE); label1.setText("First Name"); firstNameText = new Text(parent, SWT.BORDER); firstNameText.setLayoutData(gridData); Label label2 = new Label(parent, SWT.NONE); label2.setText("Last Name"); // You should not re-use GridData gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = GridData.FILL; lastNameText = new Text(parent, SWT.BORDER); lastNameText.setLayoutData(gridData); return parent; } @Override protected void createButtonsForButtonBar(Composite parent) { GridData gridData = new GridData(); gridData.verticalAlignment = GridData.FILL; gridData.horizontalSpan = 3; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalAlignment = SWT.CENTER; parent.setLayoutData(gridData); // Create Add button // Own method as we need to overview the SelectionAdapter createOkButton(parent, OK, "Add", true); // Add a SelectionListener // Create Cancel button Button cancelButton = createButton(parent, CANCEL, "Cancel", false); // Add a SelectionListener cancelButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { setReturnCode(CANCEL); close(); } }); } protected Button createOkButton(Composite parent, int id, String label, boolean defaultButton) { // increment the number of columns in the button bar ((GridLayout) parent.getLayout()).numColumns++; Button button = new Button(parent, SWT.PUSH); button.setText(label); button.setFont(JFaceResources.getDialogFont()); button.setData(new Integer(id)); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { if (isValidInput()) { okPressed(); } } }); if (defaultButton) { Shell shell = parent.getShell(); if (shell != null) { shell.setDefaultButton(button); } } setButtonLayoutData(button); return button; } private boolean isValidInput() { boolean valid = true; if (firstNameText.getText().length() == 0) { setErrorMessage("Please maintain the first name"); valid = false; } if (lastNameText.getText().length() == 0) { setErrorMessage("Please maintain the last name"); valid = false; } return valid; } @Override protected boolean isResizable() { return true; } // We need to have the textFields into Strings because the UI gets disposed // and the Text Fields are not accessible any more. private void saveInput() { firstName = firstNameText.getText(); lastName = lastNameText.getText(); } @Override protected void okPressed() { saveInput(); super.okPressed(); } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }
This
Dialog
can get called via:
MyDialog dialog = new MyDialog(shell); dialog.create(); if (dialog.open() == Window.OK) { System.out.println(dialog.getFirstName()); System.out.println(dialog.getLastName()); }
Eclipse provides several standard selection
Dialogs
in Eclipse 3.x. In Eclipse 3.x you can directly use them, in
Eclipse
4.x you may have to copy them into your plug-in and
modify them
slightly. I still list them here, so that you can use their coding as
examples.
These
Dialogs
and other useful functionality might be moved to make them directly
usable in Eclipse 4. See
Strategy for porting useful org.eclipse.ui.* bits
for Eclipse4 RCP apps Bug report
for details.
Here is a list of these
Dialogs.
ElementListSelectionDialog
ListDialog
ElementTreeSelectionDialog
CheckTreeSelectionDialog
For example
ElementListSelectionDialog
allows to select elements from a list where the
dialog provides a
filter for the elements.
ElementListSelectionDialog dialog = new ElementListSelectionDialog( shell, new LabelProvider()); dialog.setElements(new String[] { "Linux", "Mac", "Windows" }); dialog.setTitle("Which operating system are you using"); // User pressed cancel if (dialog.open() != Window.OK) { return false; } Object[] result = dialog.getResult();

This tutorial assumes what you have basic understanding of development for the Eclipse platform. Please see Eclipse 4 RCP Tutorial , Eclipse RCP Tutorial or Eclipse Plug-in Tutorial if you need any basic information.
The following demonstrates how to use the SWT
Dialogs
in an Eclipse application. We will use a
Button
in the
View
to call the SWT
dialogs.
Create a new Eclipse plug-in project called "de.vogella.rcp.dialogs.swt" with one Part (View). Create the following composite.
package de.vogella.rcp.dialogs.swt; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ColorDialog; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.FontDialog; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; public class MyComposite extends Composite { public MyComposite(final Composite parent, int style) { super(parent, style); Button button = new Button(parent, SWT.PUSH); button.setText("Press me"); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { Shell shell = parent.getShell(); openDialogs(shell); } }); } private void openDialogs(Shell shell) { // File standard dialog FileDialog fileDialog = new FileDialog(shell); // Set the text fileDialog.setText("Select File"); // Set filter on .txt files fileDialog.setFilterExtensions(new String[] { "*.txt" }); // Put in a readable name for the filter fileDialog.setFilterNames(new String[] { "Textfiles(*.txt)" }); // Open Dialog and save result of selection String selected = fileDialog.open(); System.out.println(selected); // Directly standard selection DirectoryDialog dirDialog = new DirectoryDialog(shell); dirDialog.setText("Select your home directory"); String selectedDir = dirDialog.open(); System.out.println(selectedDir); // Select Font FontDialog fontDialog = new FontDialog(shell); fontDialog.setText("Select your favorite font"); FontData selectedFont = fontDialog.open(); System.out.println(selectedFont); // Select Color ColorDialog colorDialog = new ColorDialog(shell); colorDialog.setText("Select your favorite color"); RGB selectedColor = colorDialog.open(); System.out.println(selectedColor); // Message MessageBox messageDialog = new MessageBox(shell, SWT.ERROR); messageDialog.setText("Evil Error has happend"); messageDialog.setMessage("This is fatal!!!"); int returnCode = messageDialog.open(); System.out.println(returnCode); // Message with ok and cancel button and info icon messageDialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL); messageDialog.setText("My info"); messageDialog.setMessage("Do you really want to do this."); returnCode = messageDialog.open(); System.out.println(returnCode); } }
In the method which creates your user interface, add this composite to your Part via:
new MyComposite(parent, SWT.NONE);
Run the application. If you select your button the
Dialogs
will be displayed.
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system