2012-04-24 6 views
2

NotifyDescriptor를 사용하여 팝업 대화 상자를 만드는 방법을 배웠습니다. PURCHASECASHOUT을 읽는 두 개의 큰 버튼이있는 JPanel을 디자인했으며 사용했던 코드는 YesNo이라는 하단에 다른 두 개의 버튼을 표시하고 있습니다. NotifyDescriptor가 화면에 자신의 단추를 넣고 싶지 않습니다. 버튼을 닫고 팝업을 닫고 내 맞춤 버튼 중 하나를 클릭하면 값을 저장합니다. (yes 또는 no을 클릭했을 때 창을 닫는 방법과 동일합니다.) 당신은 당신이 좀 더 컨트롤을 필요로하는 경우, 당신은에서 통과 할 수는 options 인수에 대한 String[]에 전달하거나 할 수 있습니다 중 하나 options 버튼의 텍스트를 대체하기 위해Netbeans 플랫폼의 Custom 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 대신 initialValue (NotifyDescriptor.YES_OPTION)을 사용하는 경우 String[] 값 (구매 또는 캐시 아웃) 중 하나를 사용할 수 있습니다. 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

환영과 행운을 빕니다. –

관련 문제