I'm looking into the various ways to validate input for an editable JComboBox. Currently, I need to restrict input to numbers in a specified range. Thus far I've found 3 distinct ways. Any thoughts on the best way to approach this?
JComboBox comboBox = new JComboBox(
new Object[] {"Donnie", "Danny", "Joey", "Jordan", "Jonathan"}
);
comboBox.setEditable(true);
Get control over the user input by implementing a specialized Document overriding the methods insertString and remove.
// get the combo boxes editor component JTextComponent editor = (JTextComponent) comboBox.getEditor().getEditorComponent(); // change the editor's document editor.setDocument(new BadDocument())
Replace the JTextField of JComboBox by JFormattedTextField.
You can use an input verifier as an alternative to a custom formatter
// set the input verifier setInputVerifier(verifier); class MyVerifier extends InputVerifier implements ActionListener { public boolean shouldYieldFocus(JComponent input) {} }
Thanks.