Dialogs in NetBeans RCP, DialogDisplayer class

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?

  1. Add the "Dialogs API" dependency for the module
    • invoke project properties, select libraries section and add the dependency on the "Dialogs API"
  2. Create a JPanel
    • 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())
  3. Create the NotifyDescriptor and pass the instance of the panel you have just created to it
    • You can create your own NotifyDescriptor or you can use one of the predefined ones:
      • NotifyDescriptor.Confirmation
      • NotifyDescriptor.Exception
      • NotifyDescriptor.InputLine
      • NotifyDescriptor.Message
    • 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)...
  4. Get the default instance of the Dialog displayer, call the notify method on it and evaluate the dialog result.

Few source code lines says more than thousand words;-):

        // Create instance of your panel, extends JPanel...
        MyCustomPanel myPanel = new MyCustomPanel();
        
        // Create a custom NotifyDescriptor, specify the panel instance as a parameter + other params
        NotifyDescriptor nd = new NotifyDescriptor(
                myPanel, // instance of your panel
                "Title", // title of the dialog
                NotifyDescriptor.YES_NO_OPTION, // it is Yes/No dialog ...
                NotifyDescriptor.QUESTION_MESSAGE, // ... of a question type => a question mark icon
                null, // we have specified YES_NO_OPTION => can be null, options specified by L&F,
                      // otherwise specify options as:
                      //     new Object[] { NotifyDescriptor.YES_OPTION, ... etc. },
                NotifyDescriptor.YES_OPTION // default option is "Yes"
        );

        // let's display the dialog now...
        if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
            // user clicked yes, do something here, for example:
            //     System.out.println(myPanel.getNameFieldValue());
        } 
Comments:

Hi,

Thanks for this interesting blog post. I feel dialogs shall have a Notification Line for showing more information and validation issues to the user.

NotificationLineSupport is a very good option available out of the box with NetBeans dialogs which can be used just by making two additional calls to the NotifyDescriptor before diaplaying. I feel that information will give this post additional depth and will highlight an important feature of the NetBeans dialogs.

with regards
Tushar Joshi, Nagpur

Posted by Tushar Joshi on August 16, 2009 at 02:50 AM PDT #

Post a Comment:
Comments are closed for this entry.
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today