Search in sources :

Example 11 with InputVerifier

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);
}
Also used : KeyEvent(java.awt.event.KeyEvent) DocumentListener(javax.swing.event.DocumentListener) KeyAdapter(java.awt.event.KeyAdapter) JComponent(javax.swing.JComponent) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) InputVerifier(javax.swing.InputVerifier)

Aggregations

InputVerifier (javax.swing.InputVerifier)11 JComponent (javax.swing.JComponent)11 JTextField (javax.swing.JTextField)9 JLabel (javax.swing.JLabel)8 JPanel (javax.swing.JPanel)7 Insets (java.awt.Insets)6 GridBagConstraints (java.awt.GridBagConstraints)5 GridBagLayout (java.awt.GridBagLayout)5 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 JScrollPane (javax.swing.JScrollPane)4 BorderLayout (java.awt.BorderLayout)3 FlowLayout (java.awt.FlowLayout)3 JButton (javax.swing.JButton)3 CCDDException (CCDD.CcddClassesDataTable.CCDDException)2 KeyAdapter (java.awt.event.KeyAdapter)2 KeyEvent (java.awt.event.KeyEvent)2 JCheckBox (javax.swing.JCheckBox)2 JTextArea (javax.swing.JTextArea)2 BevelBorder (javax.swing.border.BevelBorder)2