use of javax.swing.InputVerifier in project org.alloytools.alloy by AlloyTools.
the class PreferencesDialog method mkEditor.
protected JPanel mkEditor(final IntPref pref) {
final JTextField jtf = new JTextField(pref.get().toString());
jtf.setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent input) {
try {
JTextField src = (JTextField) input;
Integer.parseInt(src.getText());
return true;
} catch (NumberFormatException e) {
return false;
}
}
});
jtf.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (c < '0' || c > '9') {
// ignore event
e.consume();
}
}
});
jtf.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
updatePref();
}
@Override
public void insertUpdate(DocumentEvent e) {
updatePref();
}
@Override
public void changedUpdate(DocumentEvent e) {
updatePref();
}
private void updatePref() {
String val = jtf.getText();
try {
pref.set(Integer.parseInt(val));
} catch (NumberFormatException ex) {
}
}
});
return OurUtil.makeH(pref.title + ": ", jtf);
}
Aggregations