jComoBox input only characters (no numbers)

2019-06-11 01:09发布

问题:

I'm trying to make a jComoBox that allows all input except from digits. But when i'm trying it with jComoBox it's not working.

I did it successfully with jTextFiled(but the oppsite- no numbers):

Code of TimeKeyTyped event for i_borow jTextFiled:

private void i_borowTimeKeyTyped(java.awt.event.KeyEvent evt) {                                     
    char c = evt.getKeyChar();
    if(!( Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)) {
        evt.consume();
        l_msg2.setForeground(Color.red);
    } else {
        l_msg2.setForeground(Color.black);
    }
}

I tried to do the same to the jComoBox (c_title):

private void c_titleKeyTyped(java.awt.event.KeyEvent evt) {                                 
    System.out.println("ssss");
    char c = evt.getKeyChar();
    System.out.println(c);
    if(Character.isDigit(c)){
        evt.consume();
        l_noNum.setForeground(Color.red);
    } else {
        l_noNum.setForeground(Color.black);
    }
} 

The code doesn't work. Moreover, that's not printing the "ssss" string. Why it's not working on jComboBox? thanks.

回答1:

Here is an example how to provide the check you need for a combo box. The same approach you also can use for a text field (it's better than using of listeners). This approach works also when user pastes text into combo box.

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class FilterTryout {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frm = new JFrame("Combo test");
                JComboBox<String> combo = new JComboBox<>(new String[] {"One", "Two", "Three"});
                combo.setEditor(new ComboEditor());
                combo.setEditable(true);
                frm.add(combo);
                frm.pack();
                frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
            }
        });
    }

    private static class ComboEditor extends BasicComboBoxEditor {
        @Override
        protected JTextField createEditorComponent() {
            JTextField fld = super.createEditorComponent();
            ((AbstractDocument) fld.getDocument()).setDocumentFilter(new NoDigitsFilter());
            return fld;
        }
    }

    private static class NoDigitsFilter extends DocumentFilter {
        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (isNoDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (isNoDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean isNoDigits(String text) {
            boolean noDigits = true;
            for (int i = 0; i < text.length() && noDigits; i++) {
                noDigits = !Character.isDigit(text.charAt(i));
            }
            return noDigits;
        }
    }
}