3
我正在研究驗證可編輯JComboBox輸入的各種方法。目前,我需要將輸入限制在指定範圍內的數字。到目前爲止,我發現了三種不同的方式。任何想法來解決這個問題的最佳方式?Java - 可編輯組合框驗證
通過實施一個專門的文獻重寫方法insertString和除去越過用戶輸入控制。
// get the combo boxes editor component JTextComponent editor = (JTextComponent) comboBox.getEditor().getEditorComponent(); // change the editor's document editor.setDocument(new BadDocument())
用JFormattedTextField替換JComboBox的JTextField。
您可以使用輸入驗證作爲替代自定義格式
// set the input verifier setInputVerifier(verifier); class MyVerifier extends InputVerifier implements ActionListener { public boolean shouldYieldFocus(JComponent input) {} }
感謝。
沒有理由。剛剛注意到大量的選項,並希望更好地瞭解目前的最佳做法。 – javacavaj 2009-05-01 20:09:10