Deepak
Deepak

Reputation: 6812

Using Custom NotifyDescriptor in Netbeans Platform

I have learned to create a popup dialog box using NotifyDescriptor. I have designed a JPanel with two big buttons which reads PURCHASE and CASHOUT and the code i have used is showing another two button on the bottom which reads Yes and No. I dont want the NotifyDescriptor to put its own buttons on the screen. I just want my buttons to be there and the popup would close and store the value when one of my custom buttons is clicked.(Just like how it closes the window when yes or no is clicked). The code I am using is as follows

       // Create instance of your panel, extends JPanel...
        ChooseTransactionType popupSelector = new ChooseTransactionType();

        // Create a custom NotifyDescriptor, specify the panel instance as a parameter + other params
        NotifyDescriptor nd = new NotifyDescriptor(
                popupSelector, // 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(popupSelector.getTRANSACTION_TYPE());
        } 

Upvotes: 3

Views: 2755

Answers (1)

Jonathan Spooner
Jonathan Spooner

Reputation: 7752

In order to replace the text on the options buttons you can either pass in a String[] for the options argument or, if you need a little more control, you can pass in a JButton[]. So, in your case you need to remove the buttons from your message panel and pass in a String[] for the options argument.

For the initialValue (last argument), instead of using NotifyDescriptor.YES_OPTION you can use either one of your String[] values (Purchase or Cashout). The DialogDisplayer.notify() method will return whichever value was selected. So, in this case it would return a String but if you pass in a JButton[] then the returned value would be a JButton.

String initialValue = "Purchase";
String cashOut = "Cashout";
String[] options = new String[]{initialValue, cashOut};

NotifyDescriptor nd = new NotifyDescriptor(
            popupSelector,
            "Title",
            NotifyDescriptor.YES_NO_OPTION,
            NotifyDescriptor.QUESTION_MESSAGE,
            options,
            initialValue
    );

String selectedValue = (String) DialogDisplayer.getDefault().notify(nd);
if (selectedValue.equals(initialValue)) {
    // handle Purchase
} else if (selectedValue.equals(cashOut)) {
    // handle Cashout   
} else {
    // dialog closed with top close button
}

Upvotes: 6

Related Questions