BookNBPlatformCookbookCH0201

Contents

NetBeans Platform Cookbook Chapter 02 01

Using Dialogues

Each application needs to display some messages, choose simple answer like Yes, No, Other Reply, Cancel. Or it asks for an input. Even more complex dialogues to enter data by simple window or by wizard. NetBeans offers you a simple way how to reuse dialogues. We show standard simple dialogues and even using a custom panel in the dialogue.

Preparation

Module which uses dialogues must set dependency on Dialogs API module. Create NotifyDescriptor (or DialogDescriptor) instance and pass it into notify(nd) or notifyLater(nd) method of the DialogDisplayer class. Note all texts shown to the user would be localized by NbBundle support.

The NotifyDescriptor class has predefined options objects (OK_OPTION, CANCEL_OPTION, YES_OPTION, NO_OPTION, etc.) that you can test. The Dialog API allows your own options (e. g. your Buttons) for usage.

How to

Info / Message dialogue

        NotifyDescriptor nd = new NotifyDescriptor.Message(
                                        "You pushed button!");
        DialogDisplayer.getDefault().notify(nd);

image:Nbpcook_02_01_dialog_info.png

Figure 2.1 Info dialogue


Confirmation DialogDescriptor

        NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
                     "Save the file?", "File is not saved?");
        
        Object reply = DialogDisplayer.getDefault().notify(nd);

        if (reply == NotifyDescriptor.YES_OPTION) {
            // save file
        }
        else if (reply == NotifyDescriptor.NO_OPTION) {
            // discard changes
        }
        else if (reply == NotifyDescriptor.CANCEL_OPTION) {
            // dialog was canceled
        }
        else {
            // dialog was closed
        }

image:Nbpcook_02_02_dialog_confirm.png

Figure 2.2 Confirm Dialog

Input dialog

        NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
                                     File name", "Enter File Name" );
        nd.setInputText( "file.txt" );

        Object reply = DialogDisplayer.getDefault().notify(nd);
 
        if (reply == NotifyDescriptor.OK_OPTION) {
            String fname = getInputText(); 
        } 
        else {
            // name not given 
        }

image:Nbpcook_02_03_dialog_input.png

Figure 2.3 Input Dialog

Own options

         ... String [] OPTION_NAMES = { "First", "Second", "Third", "Fourth" };

        String optionDefault = OPTION_NAMES [0];
        NotifyDescriptor nd = new NotifyDescriptor(
                  "Which alternative?", "Choose an alternative"
                , NotifyDescriptor.OK_CANCEL_OPTION
                , NotifyDescriptor.QUESTION_MESSAGE
                , OPTION_NAMES, optionDefault);
        Object reply = DialogDisplayer.getDefault().notify(nd);

        if (reply==OPTION_NAMES [0]) {
           // do the first alternative
        }
        else if (reply==OPTION_NAMES [1]) {
           ...
        else {
           // action was canceled
        }

image:Nbpcook_02_04_dialog_options.png

Figure 2.4 Dialog with Your Own Options

Custom dialog

       initialize JButtons save, saveAsTemplate, cancel
       initialize CustomPanel for entering data
        
        bookPanel.setBook( new Book() );
  
        JButton [] options = new JButton[] { save , saveAsTemplate, cancel };

        // use DialogDescriptor (it extends NotifyDescriptor)
        final DialogDescriptor dd = new DialogDescriptor(
                     bookPanel           // component to display
                   , NbBundle.getMessage("Enter new Book")    // title
                   , true                // modal, application is blocked
                   , options             // buttons here, can be Strings, too
                   , save                  // initial value, default value
                   , DialogDescriptor.RIGHT_ALIGN  // option buttons right
                   , HelpCtx.DEFAULT_HELP
                   , null     // action listener - is set later
                );
                
        // List of options that close the dialog 
        // null - all options will close the dialog 
        // empty array - no option will close the dialog
        // - save and cancel close the dialog for us, 
        // - saveAsTemplate leaves the form for next working

        dd.setClosingOptions( new JButton[] { save, cancel } );
        
        // set action for buttons
        dd.setButtonListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        Object b = e.getSource(); 
                        if ( b == save ) {
                            saveBook( bookPanel.getBook() ); 
                        }
                        else if (b == saveAsTemplate) {
                            ...                         
                        // else cancel or closed, nothing to do
                    }

                } // PropertyChangeListener
                ); 

        DialogDisplayer.getDefault().notifyLater(dd);

image:Nbpcook_02_05_dialog_custom.png

Figure 2.5 Custom Dialog


Error message

To inform the user about thrown exception show info message with ERROR_MESSAGE type and write important information into app log.


                       
        // NotifyDescriptor.Exception is deprecated !!!
        // use logging
        Logger.getLogger(getClass().getName()).throwing(
                  getClass().getName() 
                , "errorButtonActionPerformed"
                , npe);
        // message should be localized, of course!
            NotifyDescriptor nd = new NotifyDescriptor.Message(
                    NbBundle.getMessage(DialogsTopComponent.class,   
                                            "HTML_SEVERE ERROR OCCURED_BR")
                    + NbBundle.getMessage(DialogsTopComponent.class,
                                             "CONTACT_ADMINISTRATOR")
                    + NbBundle.getMessage(DialogsTopComponent.class,
                                         "AND_CLOSE_APPLICATION.")
                 , NotifyDescriptor.ERROR_MESSAGE);
            // display dialog in other thread
            DialogDisplayer.getDefault().notifyLater(nd);
            // or display Message dialog the application is closing
            // and close it by
            //    LifecycleManager.getDefault().exit();

HTML_SEVERE_ERROR_OCCURED_BR=<html>Severe error occured<br>
CONTACT_ADMINISTRATOR=<b>Contact the program administrator </b><br><br>
AND_CLOSE_APPLICATION=and <b>close</b> this application.

image:Nbpcook_02_dialog_error.png

Figure 2.2 Error message


Tip - sources

Text and sources were created under NB 6.8 6.9, 7.0, 7.1, 7.2.

Navigation

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo