How to make InputVerifier work with editable JComboBox

I have JComboBoxwith custom inputVerifyer, set to limit MaxLength when it is configured to edit.

The verification method is never called. The same validation request is invoked with a value JTextField.

What can i do wrong?

+3
source share
2 answers

I found a workaround. I thought that I would let this person know this problem.

Basically. Instead of setting the inputVerifier to the ComboBox, you set the "Component Editor" for it.

JComboBox combo = new JComboBox();
JTextField tf = (JTextField)(combo.getEditor().getEditorComponent());
tf.setInputVerifier(verifyer);
+8
source

Show us a small section of your code.

package inputverifier;

import javax.swing.*;

    class Go {
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
            runEDT();
        }});
    }
    private static void runEDT() {
        new JFrame("combo thing") {{
            setLayout(new java.awt.GridLayout(2, 1));
            add(new JComboBox() {{
                setEditable(true);
                setInputVerifier(new InputVerifier() {
                    @Override public boolean verify(JComponent input) {
                        System.err.println("Hi!");
                        return true;
                    }
                });
            }});
            add(new JTextField());
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }};
    }    
}

, , JComboBox . .

+1

Source: https://habr.com/ru/post/1696780/


All Articles