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