Discover Eclipse's JFace Dialogs
Join the DZone community and get the full member experience.
Join For Freethe jface framework of eclipse provides many standard and useful dialogs and a framework to build custom dialogs and wizards. when the standard dialogs seems to be too simple for your plugin or rcp developement , one can extend the standard dialogs to suit their own needs. the aim of this article is to provide example oriented approach to dialogs and see in depth of all frequently used dialogs. i am hoping to have this article as the point of reference for many developers to get an overview of dialogs.
dialogs are modal ui components which would do some user interaction tasks. traditionally dialogs are used to enter a simple input, to select a choice from a list, to select multiple nodes from a tree, to inform about a success/failure from an operation, to input confirmations from the user, and so on.
eclipse provides standard dialogs from two different frameworks, swt provides very basic dialogs like fontdialog , colordialog , directorydialog , filedialog and fontdialog . these swt dialogs are coming out of the swt as they are very basic and has close connection to the os. all these dialogs are available from org.eclipse.swt.widgets package from the org.eclipse.swt.<platform>.jar.
jface provides a variety of dialogs for the implementors to extend or to use them directly. its interesting to look into variety of dialogs provided by jface and i would leave the usage to your imagination and context. rest of this article is mostly code, screen shots and less of textual description. i tried to keep the short, sweet and simple.
filedialog
this is a swt dialog but worth mentioning under the general concept of the dialogs.
"instances of this class allow the user to navigate the file system and select or enter a file name. note: only one of the styles save and open may be specified. important: this class is intended to be subclassed <em>only</em> within the swt implementation."
filedialog fsd = new filedialog(shell);
fsd.setfilterextensions(new string[] {"*.jpg"});
fsd.settext("select my jpg files...");
fsd.open();
information dialogs
these dialogs are used to inform user about some event and take some necessary action. there are different dialogs under this category.
errordialog
" a dialog to display one or more errors to the user, as contained in an istatus object. if an error contains additional detailed information then a details button is automatically supplied, which shows or hides an error details viewer when pressed by the user. "
errordialog.openerror(shell, "this is error title", "this is error message", status);
messagedialog
" a dialog for showing messages to the user. this concrete dialog class can be instantiated as is, or further subclassed as required. "
messagedialog.openinformation(shell, "message dialog information", "this is some random message");
messagedialog.openquestion(shell, "this is a tricky question", "do you know whats 2 + 3 = ?");
inputdialog
this dialog is used for a very general paradigm to process some input from the user. generally its not recommended to use this for input of complex data and for large number of input fields.
final inputdialog id = new inputdialog(shell, "enter your name", "your name will be entered in nobel prize list.", "<your name>", new iinputvalidator() {
public string isvalid(string newtext) {
if (newtext.contains("x")) {
return "your name can not have \"x\" character.";
}
return null;
}
});
id.open();
printerdialog
" instances of this class allow the user to select a printer and various print-related parameters prior to starting a print job. important: this class is intended to be subclassed only within the swt implementation. "
printdialog pd = new printdialog(shell);
pd.open();
progressmonitordialog
this is a very important dialog component most of the applications would use. this dialog is used to represent any time consuming task or to provide a long running workspace task to the user. proper care must be taken to show accurate information of progress information to the user.
progressmonitordialog pmd = new progressmonitordialog(shell);
pmd.setcancelable(true);
pmd.create();
try {
pmd.run(true, true, new mysimplerunnable());
} catch (invocationtargetexception e) {
e.printstacktrace();
} catch (interruptedexception e) {
e.printstacktrace();
}
titleareadialog
" a dialog that has a title area for displaying a title and an image as well as a common area for displaying a description, a message, or an error message. this dialog class may be subclassed. "
this is the class that i often use for "about mycompany" kind of dialogs.
public class titleareadialog_ex extends titleareadialog {
public titleareadialog_ex(shell parentshell) {
super(parentshell);
}
@override
protected control createdialogarea(composite parent) {
composite contents = new composite(parent, swt.none);
contents.setlayoutdata(new griddata(swt.fill, swt.fill, true, true));
settitle("this is the title area title");
setmessage("this is the title area description");
new label(contents, swt.left).settext("hello world in content area");
dialog.applydialogfont(parent);
point defaultmargins = layoutconstants.getmargins();
gridlayoutfactory.filldefaults().numcolumns(2).margins(defaultmargins.x, defaultmargins.y).generatelayout(contents);
return contents;
}
public static void main(string[] args) {
display display = new display();
shell shell = new shell(display);
shell.settext("title area shell");
shell.pack();
titleareadialog tadialog = new titleareadialog_ex(shell);
tadialog.settitleareacolor(new rgb(0, 100, 200));
tadialog.open();
while (!shell.isdisposed()) {
if (!display.readanddispatch())
display.sleep();
}
display.dispose();
}
}
elementlistselectiondialog
" a class to select elements out of a list of elements. "
elementlistselectiondialog listdialog = new elementlistselectiondialog(shell,new labelprovider());
listdialog.settitle("this is element list title");
listdialog.setmessage("this is element list message");
listdialog.setelements(list.toarray());
listdialog.open();
listdialog
" a dialog that prompts for one element out of a list of elements. uses istructuredcontentprovider to provide the elements and ilabelprovider to provide their labels. "
listdialog ld = new listdialog(shell);
ld.setaddcancelbutton(true);
ld.setcontentprovider(new arraycontentprovider());
ld.setlabelprovider(new labelprovider());
ld.setinput(list.toarray());
ld.setinitialselections(list.toarray());
ld.settitle("select # of nobel prize nominations :");
ld.open();
twopaneelementselector
" a list selection dialog with two panes. duplicated entries will be folded together and are displayed in the lower pane (qualifier) ."
twopaneelementselector elementselector = new twopaneelementselector(shell, labelprovider, labelprovider);
elementselector.settitle("two pane element selector");
elementselector.setelements(new string[] {"one", "two", "three"});
elementselector.setlowerlistlabel("lower list");
elementselector.setupperlistlabel("upper list");
elementselector.setmultipleselection(true);
elementselector.setinitialselections(new string[] {"one", "two"});
elementselector.open();
list selectiondialog
" a standard dialog which solicits a list of selections from the user. this class is configured with an arbitrary data model represented by content and label provider objects. the getresult method returns the selected elements. "
listselectiondialog lsd = new listselectiondialog(window.getshell(), list, new arraycontentprovider(), new labelprovider(), "listselectiondialog message");
lsd.setinitialselections(list.toarray());
lsd.settitle("select # of nobel prize nominations :");
lsd.open();
elementtreeselectiondialog
" a dialog class to select elements out of a tree structure. "
elementtreeselectiondialog treedialog = new elementtreeselectiondialog(window.getshell(), new mylabelprovider(), new mytreecontentprovider());
treedialog.setinput(nullfamily);
treedialog.setinitialselections(nullfamily.getchildren());
treedialog.setinitialelementselections(list);
treedialog.setinitialselection("one");
treedialog.settitle("elementtreeselection title");
treedialog.setmessage("elementtreeselectiondialog message");
treedialog.open();
checkedtreeselectiondialog
"a dialog class to select elements out of a tree structure"
checkedtreeselectiondialog checkedtreedialog = new checkedtreeselectiondialog(window.getshell(), new mylabelprovider(), new mytreecontentprovider());
checkedtreedialog.setinput(nullfamily);
checkedtreedialog.setinitialselections(nullfamily.getchildren());
checkedtreedialog.setinitialelementselections(list);
checkedtreedialog.setinitialselection("one");
checkedtreedialog.settitle("elementtreeselection title");
checkedtreedialog.setmessage("elementtreeselectiondialog message");
checkedtreedialog.open();
some notes
- yesnocancellistselectiondialog has been deprecated in 3.3 of eclipse.
-
rcp product extensions and custmizations can be done via aboutdialogs.
- aboutdialog
- aboutfeaturesdialog
- aboutpluginsdialog
- aboutsystemdialog
- printdialog is intended to be sub-classes by the clients and use it for identifying the printers and other parameters for print job.
- saveasdialog is not available in eclipse 3.3
conclusion
dialogs are a great way to present simple data and receive user input. its very important to understand several dialogs provided by jface and use them appropriately.
more information
Published at DZone with permission of Suresh Krishna. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
WireMock: The Ridiculously Easy Way (For Spring Microservices)
-
What Is JHipster?
-
The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
-
VPN Architecture for Internal Networks
Comments