2011-10-12 21 views
2

我有一個JDialog創建in a fashion like this,相應的Oracle tutorial暫時禁用JDialog上的確定按鈕,相應地JTextField驗證

使用JOptionPane構造函數:

optionPane = new JOptionPane(array, 
       JOptionPane.QUESTION_MESSAGE, 
       JOptionPane.YES_NO_OPTION, 
       null, 
       options, 
       options[0]); 

我沒有提到的「是」和「否」按鈕,因爲它們是由JOptionPane的構造函數創建。

現在,在我的對話我有一個JFormattedText場由我創建的時間可持續驗證文本字段輸入InputValidator:

public class ValidatedDoubleField extends InputVerifier implements DocumentListener { 

    private JTextField field; 
    private Border defaultBorder; 
    public ValidatedDoubleField(JFormattedTextField f){ 
     this.field = f; 
     this.defaultBorder = f.getBorder(); 
     f.getDocument().addDocumentListener(this); 
    } 
    @Override 
    public boolean verify(JComponent input) { 
     //System.out.println("verify"); 
     if (input instanceof JTextField){ 
      JTextField f = (JTextField)input; 

      try{ 
       Double value = Double.parseDouble(f.getText().replace(',', '.')); 
       return true; 
      }catch (NumberFormatException e){ 
       return false; 
      } 
     }else if (input instanceof JFormattedTextField){ 
      JFormattedTextField f = (JFormattedTextField)input; 

      try{ 
       Double value = Double.parseDouble(f.getText().replace(',', '.')); 
       return true; 
      }catch (NumberFormatException e){ 
       return false; 
      } 
     } 
     return false; 
    } 
    public boolean shouldYieldFocus(JComponent input){ 

     boolean inputOK = verify(input); 
     if (inputOK) { 
      if (input instanceof JTextField){ 

       JTextField f = (JTextField)input; 
       f.setBorder(defaultBorder); 
       return true; 
      }else if (input instanceof JFormattedTextField){ 

       JFormattedTextField f = (JFormattedTextField)input; 

       f.setBorder(defaultBorder); 
       return true; 
      }else 
       return false; 
     } else { 
      if (input instanceof JTextField){ 

       JTextField f = (JTextField)input; 

       f.setBorder(BorderFactory.createLineBorder(Color.red)); 
       Toolkit.getDefaultToolkit().beep(); 
       return false; 
      }else if (input instanceof JFormattedTextField){ 

       JFormattedTextField f = (JFormattedTextField)input; 

       f.setBorder(BorderFactory.createLineBorder(Color.red)); 
       Toolkit.getDefaultToolkit().beep(); 
       return false; 
      }else 
       return false; 
     } 
     //return true; 

    } 
    @Override 
    public void changedUpdate(DocumentEvent e) { 

     this.field.getInputVerifier().shouldYieldFocus(field); 
    } 
    @Override 
    public void insertUpdate(DocumentEvent e) { 

     this.field.getInputVerifier().shouldYieldFocus(field); 
    } 
    @Override 
    public void removeUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 
     this.field.getInputVerifier().shouldYieldFocus(field); 
    } 

} 

我張貼,即使它不是一個問題,以便相關的InputVerifier代碼。

現在我想要做的是暫時禁用「確定」按鈕,直到該字段將被驗證,但我沒有提及它。

我該怎麼做?

我在尋找類似:

JButton b = optionPane.getOkButton(); 
if (myFieldNotValidate) 
    b.setEnabled(false); 
+2

指針[這裏](http://stackoverflow.com/questions/5877994/how-to-change-the-button-backgrounds-inside-joptionpane) –

+0

@ring承載:謝謝。我用camickr解決方案解決了問題。那很棒! – Heisenbug

回答

3

你可以嘗試這樣的事情在JOptionPane的對話框來定位按鈕。

public class Snippet { 

    public static void main(String[] args) { 
     JOptionPane optionPane = new JOptionPane("Test", 
         JOptionPane.QUESTION_MESSAGE, 
         JOptionPane.YES_NO_OPTION, 
         null); 

     List<JButton> buttons = new ArrayList<JButton>(); 

     loadButtons(optionPane, buttons); 

    } 

    public static void loadButtons(JComponent comp, List<JButton> buttons) { 
     if (comp == null) { 
      return;  
     } 

     for (Component c : comp.getComponents()) { 
      if (c instanceof JButton) { 
       buttons.add((JButton) c); 

      } else if (c instanceof JComponent) { 
       loadButtons((JComponent) c, buttons); 
      } 
     } 
    } 

}