Dialogs in NetBeans RCP, DialogDisplayer class
By Petr Dvorak on Jun 16, 2009
Friends of mine who tried to play around with the NetBeans platform usually didn't know how to correctly create a custom dialog. They tend to reason about this task in the basic "swingy" way: create a new JOptionPane, set it visible when needed and hide it when not needed anymore.
This approach certainly works and maybe it is more understandable from the beginner's point of view. On the other hand, the NetBeans platform has much better and more structured way of displaying a dialog.
The classes that are important for this are NotifyDescriptor and DialogDisplayer:
- NotifyDescriptor is a class that carries the information about how the dialog should look (the actual content, the main buttons, etc.).
- DialogDisplayer is a (singleton) class that actually displays the dialog that is described by the NotifyDescriptor and handles the return value of the dialog.
So what do you need to do in order to create a dialog in NetBeans?
- Add the "Dialogs API" dependency for the module
- invoke project properties, select libraries section and add the dependency on the "Dialogs API"
- Design the content of the JPanel
- Do not create the OK/Cancel/Yes/No/etc. buttons - those are handled in the NotifyDescriptor (later)
- Create the public API for getting some values from the JPanel (for example method like getNameFieldValue())
- You can create your own NotifyDescriptor or you can use one of the predefined ones:
- Tip: If the description of methods makes no sense since there is no reasonable JavaDoc and if the parameter names are not saying too much, go to Tools>Plugins, select Available Plugins tab and install "NetBeans API Documentation" plugin. This will install JavaDoc for the NetBeans Platform API and the descriptions of the constructors should be more comprehensive (and coding with NB Platform becomes more pleasant)...
Few source code lines says more than thousand words: