2009-05-01 59 views
3

我正在研究驗證可編輯JComboBox輸入的各種方法。目前,我需要將輸入限制在指定範圍內的數字。到目前爲止,我發現了三種不同的方式。任何想法來解決這個問題的最佳方式?Java - 可編輯組合框驗證

​​
  1. 通過實施一個專門的文獻重寫方法insertString和除去越過用戶輸入控制。

    // get the combo boxes editor component 
    JTextComponent editor = 
         (JTextComponent) comboBox.getEditor().getEditorComponent(); 
    // change the editor's document 
    editor.setDocument(new BadDocument()) 
    
  2. 用JFormattedTextField替換JComboBox的JTextField。

  3. 您可以使用輸入驗證作爲替代自定義格式

    // set the input verifier 
    setInputVerifier(verifier); 
    
    class MyVerifier extends InputVerifier implements ActionListener 
    { 
        public boolean shouldYieldFocus(JComponent input) {} 
    } 
    

感謝。

回答

2

這就是InputVerifier的設計目的。我會從其中一個開始,但真的應該這樣做。您的要求有什麼特別的原因,爲什麼它不起作用?

+0

沒有理由。剛剛注意到大量的選項,並希望更好地瞭解目前的最佳做法。 – javacavaj 2009-05-01 20:09:10