【发布时间】:2009-05-01 19:21:22
【问题描述】:
我正在研究验证可编辑 JComboBox 输入的各种方法。目前,我需要将输入限制为指定范围内的数字。到目前为止,我已经找到了 3 种不同的方法。对解决此问题的最佳方法有何想法?
JComboBox comboBox = new JComboBox(
new Object[] {"Donnie", "Danny", "Joey", "Jordan", "Jonathan"}
);
comboBox.setEditable(true);
-
通过实现覆盖方法 insertString 和 remove 的专用 Document 来控制用户输入。
// get the combo boxes editor component JTextComponent editor = (JTextComponent) comboBox.getEditor().getEditorComponent(); // change the editor's document editor.setDocument(new BadDocument()) 将JComboBox的JTextField替换为JFormattedTextField。
-
您可以使用输入验证器来替代自定义格式化程序
// set the input verifier setInputVerifier(verifier); class MyVerifier extends InputVerifier implements ActionListener { public boolean shouldYieldFocus(JComponent input) {} }
谢谢。
【问题讨论】:
标签: java validation jcombobox