jComoBox仅输入字符(无数字) [英] jComoBox input only characters (no numbers)

查看:52
本文介绍了jComoBox仅输入字符(无数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个jComoBox,它允许除数字之外的所有输入.但是当我用jComoBox尝试时,它不起作用.

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.

我使用jTextFiled成功完成了该操作(但是没有-没有数字):

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

i_borow jTextFiled的TimeKeyTyped事件的代码:

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);
    }
}

我试图对jComoBox(c_title)做同样的事情:

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);
    }
} 

该代码不起作用. 而且,这不是在打印"ssss"字符串. 为什么在jComboBox上不起作用? 谢谢.

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

推荐答案

这里是一个示例,说明如何提供组合框所需的支票.您也可以对文本字段使用相同的方法(比使用侦听器更好).当用户将文本粘贴到组合框中时,这种方法也适用.

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;
        }
    }
}

这篇关于jComoBox仅输入字符(无数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆