Java-可编辑组合框验证
我正在研究各种方法来验证可编辑JComboBox的输入。目前,我需要将输入限制为指定范围内的数字。到目前为止,我发现了三种不同的方法。你有没有想过最好的方法Java-可编辑组合框验证,java,validation,jcombobox,Java,Validation,Jcombobox,我正在研究各种方法来验证可编辑JComboBox的输入。目前,我需要将输入限制为指定范围内的数字。到目前为止,我发现了三种不同的方法。你有没有想过最好的方法 JComboBox comboBox = new JComboBox( new Object[] {"Donnie", "Danny", "Joey", "Jordan", "Jonathan"} ); comboBox.setEditable(true); 通过实现一个专门的文档覆盖insertString和remove方法来
JComboBox comboBox = new JComboBox(
new Object[] {"Donnie", "Danny", "Joey", "Jordan", "Jonathan"}
);
comboBox.setEditable(true);
// get the combo boxes editor component
JTextComponent editor =
(JTextComponent) comboBox.getEditor().getEditorComponent();
// change the editor's document
editor.setDocument(new BadDocument())
// set the input verifier
setInputVerifier(verifier);
class MyVerifier extends InputVerifier implements ActionListener
{
public boolean shouldYieldFocus(JComponent input) {}
}
谢谢。这就是InputVerifier的设计目的。我想从其中的一个开始,但真的应该这样做。您的要求中有没有任何特定的原因说明它为什么不起作用?没有原因。我只是注意到了大量的选项,并想更好地了解当前的最佳实践是什么。再仔细考虑一下,我实际上想阻止输入除数字以外的任何内容。InputVerifier仅在失去焦点之前进行检查。输入每个字符时如何进行检查?谢谢。好吧,如果你需要“实时”的话,我会选择自定义文档方法