JCombobox 只接受字母 [英] JCombobox which accepts only alphabets

查看:37
本文介绍了JCombobox 只接受字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个只接受字母和长度为 3 的组合框?我创建了一个自动完成组合框,将 editable 设置为 true.现在我想允许用户只在组合框的输入字段中输入长度为 3 的字母.有什么想法吗?

How to create a combo box which accepts only alphabets and of length 3? I have created an auto completion combo box which set editable to true. Now I want to allow the user to only enter alphabets with length 3 to the input field of a combobox. Any ideas pls?

我将此文档过滤器添加到 Jcombobox 的文本字段中.

I added this document filter to textfield of Jcombobox.

class AlphaDocumentFilter extends DocumentFilter {

  private final static Pattern CHARACTERS = Pattern.compile("[a-zA-Z]");

      public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,
          AttributeSet attr) throws BadLocationException {
        if (text != null && CHARACTERS.matcher(text).matches() && (fb.getDocument().getLength() + text.length()) <= 3){
          fb.insertString(offset, text.toUpperCase(), attr);
        }

      }

      public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
          AttributeSet attrs) throws BadLocationException {
        if (text != null && CHARACTERS.matcher(text).matches() && (fb.getDocument().getLength() + text.length() - length) <= 3){
          fb.replace(offset, length, text.toUpperCase(), attrs);
        }

      }
    }

如果我在组合框中输入值,它会起作用.但是当我选择项目时,它不会显示在组合框中,因为过滤器允许它.

It works if I enter values in combobox. But when I select item it is not getting displayed in combobox because filter dint allow it to.

例如,如果我在组合框编辑器中输入 EUR,它就可以工作.但是如果我从项目中选择 EUR 它不起作用意味着过滤器不允许它显示在编辑器中.

For eg, if I enter EUR in combobox editor it works. but if I select EUR from the items it doesnt work meaning filter doesnt allow it to display in the editor.

我错过了什么吗?

推荐答案

您需要访问组合框的编辑器才能过滤字符.组合框的默认编辑器是文本字段.您可以使用以下方法访问文本字段:

You need to access the editor of the combo box so you can filter the characters. The default editor of a combo box is a text field. You can access the text field using:

ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();

过滤字符的最佳方法是在文本字段的Document 上使用DocumentFilter.阅读 Swing 教程中关于实现文档过滤器的部分.本教程包含一个工作过滤器,用于限制可以输入的字符数.

The best way to filter the characters is to use a DocumentFilter on the Document of the text field. Read the section from the Swing tutorial on Implementing a DocumentFilter. The tutorial contains a working filter to limit the number of character that can be entered.

您需要修改过滤器以只允许使用字母字符.请注意,您需要修改 replace()insertString() 方法,因为这两个方法中的任何一个都可能用于将文本添加到 Document>.

You will need to modify the filter to only allow alphabetic characters. Note you will need to modify the replace() and insertString() methods, since either of those methods might be used to add text to a Document.

阅读 Swing 教程中关于如何使用 Combo 的部分盒子.有一个使用可编辑组合框的示例.请注意该部分中的评论:

Read the section from the Swing tutorial on How to Use Combo Boxes. There is an example that uses an editable combo box. Note the comment in that section:

当用户从菜单中选择一个项目并且当用户输入 Enter 时,一个可编辑的组合框会触发一个动作事件.请注意,当用户在组合框中输入值时,菜单保持不变.如果需要,您可以轻松编写一个动作侦听器,在用户每次输入唯一值时向组合框的菜单中添加一个新项目.

这篇关于JCombobox 只接受字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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