无法编写可编辑的JComboBox [英] Can not write editable JComboBox

查看:54
本文介绍了无法编写可编辑的JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用可编辑的JComboBox在数据库中进行搜索,但是在编写时,只接受给我写一个字母和任意数量的数字,我该怎么做才能允许我写字母和数字? /p>

以下代码仅接受数字,退格键,输入键,但不接受字母.

comboBusqueda.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {

    public void keyPressed(KeyEvent evt) {

        String cadenaEscrita = comboBusqueda.getEditor().getItem().toString();

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
           if(comparar(cadenaEscrita)){
               buscar(cadenaEscrita);
           }else{
                buscar(comboBusqueda.getSelectedItem().toString());
                comboBusqueda.getSelectedItem();

            }
        }
        if (evt.getKeyCode() >= 65 && evt.getKeyCode() <= 90 
                || evt.getKeyCode() >= 96 && evt.getKeyCode() <= 105 
                || evt.getKeyCode() == 8
                || evt.getKeyCode() == KeyEvent.VK_ENTER
                ) {
            comboBusqueda.setModel(dc.getLista(cadenaEscrita));
            if (comboBusqueda.getItemCount() > 0) {
                comboBusqueda.getEditor().setItem(cadenaEscrita);
                comboBusqueda.showPopup();                     

            } else {
                comboBusqueda.addItem(cadenaEscrita);
            }
        }
    }
});

解决方案

这里是JComboBox,允许用户仅输入(英文)字母和数字以及_字符:

public class ComboBoxExample extends JPanel {

    ComboBoxExample() {

        JComboBox<String> cb = new JComboBox<>();
        cb.setEditable(true);
        cb.setEditor(new FilterCBEditor());
        add(cb);
    }

    class FilterCBEditor extends BasicComboBoxEditor {

        FilterCBEditor() {

            ((AbstractDocument) editor.getDocument()).setDocumentFilter(new DocumentFilter() {

                @Override
                public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {

                    if (string.matches("\\w+"))
                        super.insertString(fb, offset, string, attr);
                }

                @Override
                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

                    if (text.matches("\\w+"))
                        super.replace(fb, offset, length, text, attrs);
                }
            });
        }
    }
}

这个想法是为组合框设置一个新的编辑器,并在其文本字段中添加一个文档过滤器.

I'm doing a search on the database using an editable JComboBox, but when it comes to writing, only accepts write me a letter and any amount of numbers, how I can do that allows me to write letters and numbers?

The following code accepts only numbers, backspace key, enter key, but not letters.

comboBusqueda.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {

    public void keyPressed(KeyEvent evt) {

        String cadenaEscrita = comboBusqueda.getEditor().getItem().toString();

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
           if(comparar(cadenaEscrita)){
               buscar(cadenaEscrita);
           }else{
                buscar(comboBusqueda.getSelectedItem().toString());
                comboBusqueda.getSelectedItem();

            }
        }
        if (evt.getKeyCode() >= 65 && evt.getKeyCode() <= 90 
                || evt.getKeyCode() >= 96 && evt.getKeyCode() <= 105 
                || evt.getKeyCode() == 8
                || evt.getKeyCode() == KeyEvent.VK_ENTER
                ) {
            comboBusqueda.setModel(dc.getLista(cadenaEscrita));
            if (comboBusqueda.getItemCount() > 0) {
                comboBusqueda.getEditor().setItem(cadenaEscrita);
                comboBusqueda.showPopup();                     

            } else {
                comboBusqueda.addItem(cadenaEscrita);
            }
        }
    }
});

解决方案

Here is a JComboBox which allows users to enter only (English) letters and numbers and the _ character:

public class ComboBoxExample extends JPanel {

    ComboBoxExample() {

        JComboBox<String> cb = new JComboBox<>();
        cb.setEditable(true);
        cb.setEditor(new FilterCBEditor());
        add(cb);
    }

    class FilterCBEditor extends BasicComboBoxEditor {

        FilterCBEditor() {

            ((AbstractDocument) editor.getDocument()).setDocumentFilter(new DocumentFilter() {

                @Override
                public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {

                    if (string.matches("\\w+"))
                        super.insertString(fb, offset, string, attr);
                }

                @Override
                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

                    if (text.matches("\\w+"))
                        super.replace(fb, offset, length, text, attrs);
                }
            });
        }
    }
}

The idea is to set a new editor for the combo box and add a document filter to its text field.

这篇关于无法编写可编辑的JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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