2012-04-24 10 views
2

NotifyDescriptorを使用してポップアップダイアログボックスを作成する方法を学びました。 PURCHASECASHOUTという2つの大きなボタンを持つJPanelを設計しました。私が使用したコードは、YesNoの2つのボタンが表示されています。私はNotifyDescriptorが画面上に独自のボタンを置くことを望みません。私のボタンがそこにあるようにしたいのですが、私のカスタムボタンがクリックされたときにポップアップが閉じて値を保存するようにしてください(yesまたはnoをクリックしたときのウィンドウの閉じ方と同じです)。あなたはもう少しコントロールが必要な場合は、あなたが渡すことができ、options引数にString[]に渡したりすることができますいずれかoptionsボタンのテキストを置き換えるためにNetbeansプラットフォームでカスタムNotifyDescriptorを使用する

 
     // 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()); 
     } 

答えて

4

を次のように私が使用していたコードですa JButton[]。あなたのケースでは、messageパネルからボタンを削除し、options引数の場合はString[]を渡す必要があります。

NotifyDescriptor.YES_OPTIONの代わりに、String[]のいずれかの値(購入またはキャッシュアウト)を使用できます。最後の引数はinitialValueです。 DialogDisplayer.notify()メソッドは、選択された値を返します。したがって、この場合は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 
} 
+0

優秀!!これは私が探していたものです..それに感謝!! – Deepak

+0

あなたの歓迎と幸運 –

関連する問題