简体   繁体   English

在Netbeans平台中使用自定义NotifyDescriptor

[英]Using Custom NotifyDescriptor in Netbeans Platform

I have learned to create a popup dialog box using NotifyDescriptor. 我已经学会了使用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 . 我设计了一个带有两个大按钮的JPanel,分别显示PURCHASECASHOUT ,我使用的代码在底部显示了另外两个按钮,分别显示YesNo I dont want the NotifyDescriptor to put its own buttons on the screen. 我不希望NotifyDescriptor在屏幕上放置其自己的按钮。 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). 我只希望我的按钮在那里并且单击我的自定义按钮之一时弹出窗口将关闭并存储值(就像单击“ yes或“ no时如何关闭窗口一样)。 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());
        }

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[] . 为了替换options按钮上的文本,您可以传递String[]作为options参数,或者,如果需要更多控制权,则可以传递JButton[] So, in your case you need to remove the buttons from your message panel and pass in a String[] for the options argument. 因此,在您的情况下,您需要从message面板中删除按钮,并为options参数传入String[]

For the initialValue (last argument), instead of using NotifyDescriptor.YES_OPTION you can use either one of your String[] values (Purchase or Cashout). 对于initialValue (最后一个参数),可以使用String[]值之一(购买或NotifyDescriptor.YES_OPTION ),而不是使用NotifyDescriptor.YES_OPTION The DialogDisplayer.notify() method will return whichever value was selected. DialogDisplayer.notify()方法将返回选择的任何值。 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但是如果您传递JButton[]则返回的值将是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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM