java - jComboBox 只能输入字符(不能输入数字)

标签 java swing jcombobox

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

我使用 jTextFiled 成功完成了此操作(但相反的站点 - 没有数字):

i_borow jTextFiled 的 TimeKeyTyped 事件代码:

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) 执行相同的操作:

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 上不起作用? 谢谢。

最佳答案

以下是如何提供组合框所需的检查的示例。您也可以将相同的方法用于文本字段(它比使用监听器更好)。当用户将文本粘贴到组合框中时,此方法也适用。

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

关于java - jComboBox 只能输入字符(不能输入数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431798/

相关文章:

java - 我想通过Java中的客户端服务器套接字将数据附加到现有文件中

c# - 不使用 Visual Studio GUI 设计器(工具箱)构建 C# GUI

java - JComboBox 改变形状和颜色

java - 背景与 getResourceAsStream ("path")

java - 根据不存在的特定记录从 MySQL 检索数据

java - getItem 和 getSelectedItem 之间的区别

java - 如何将某个位置的字符替换为某个字母

与语言环境相关的 Java DynamicReports 格式日期

java - 主线程完成时优雅地关闭 ExecutorService

java - java中随机图像(d)按钮的确切数量