2013-05-18 80 views
1

當我按下抱怨框時,每個輸入對話框顯示兩次...我試圖刪除boxComplain.setSelected(true),它工作(它只顯示一次),但這使複選框去我輸入輸入後。JOptionPane.showInputDialog顯示兩次

class CheckBoxListener implements ItemListener { 

    public void itemStateChanged(ItemEvent event) { 
    if(boxComplain.isSelected()) 

     { 
      ab=JOptionPane.showInputDialog("Enter Reason of Complain: "); 
      ac=JOptionPane.showInputDialog("Enter What The Complain is About: "); 
      label4.setText("Complain request"); 
      boxComplain.setSelected(true); 
     } 
     } 
     } 
+3

這聽起來像你所註冊的'ActionListener'兩次 – MadProgrammer

+0

沒有我添加了一個checkboxlistener到boxcomplain只有一次我確定 –

回答

3

ItemListener被調用兩次 - 一次是原始選擇已被更改,第二次是新的選擇被註冊。考慮改用ActionListener。

另一個竅門是刪除和添加的ItemListener:

 public void itemStateChanged(ItemEvent event) { 
     if(boxComplain.isSelected()) { 
      ab=JOptionPane.showInputDialog("Enter Reason of Complain: "); 
      ac=JOptionPane.showInputDialog("Enter What The Complain is About: "); 
      label4.setText("Complain request"); 
      boxComplain.removeItemListener(this); 
      boxComplain.setSelected(true); 
      boxComplain.addItemListener(this); 
     } 
    } 
+0

確定虐待嘗試 –

+0

@ user1950784不客氣! –

+1

@ user1950784或測試SELECTED/DESELECTED內itemStateChanged – mKorbel