Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java IDocumentFilter使用HashMap作为字典菜单,在JTextField上进行验证_Java_Swing_Validation_Hashmap_Jtextfield - Fatal编程技术网

Java IDocumentFilter使用HashMap作为字典菜单,在JTextField上进行验证

Java IDocumentFilter使用HashMap作为字典菜单,在JTextField上进行验证,java,swing,validation,hashmap,jtextfield,Java,Swing,Validation,Hashmap,Jtextfield,我有一个Hashmap,其中填充了Sybase中一个表中的信息,结构如下 -索引字符 ----1----1234567890 ----2-----abcdefg 我正在尝试设置一个从JtextField扩展而来的新CustomJTextField,因此该控件将有一个名为MASK的atribute,我可以在该属性中设置掩码编号,如下所示: customtextField = new CustomTextField(20); customtextField.set_MASK(1); 我已经让c

我有一个Hashmap,其中填充了Sybase中一个表中的信息,结构如下

-索引字符

----1----1234567890

----2-----abcdefg

我正在尝试设置一个从JtextField扩展而来的新CustomJTextField,因此该控件将有一个名为MASK的atribute,我可以在该属性中设置掩码编号,如下所示:

 customtextField = new CustomTextField(20);
 customtextField.set_MASK(1);
我已经让customtextField处理了一些额外的属性, mask atribute的行为不允许用户写入表中未包含的字母,因此如果customtextField设置为mask(1),则用户只能写入数字

我需要使用DocumentFilter的帮助,或者任何建议,我需要从数据库的表中获取字典,(用户需求)

编辑*

通过推荐,我试图获得一个DocumentFilter示例,该示例只允许使用数组中包含的类型字符(从HAshMap创建)

您可能希望改用

这将允许您构造限制用户实际键入内容的筛选器,而不是依赖于后期验证

查看一些示例

已更新

这非常简单。使用我链接的示例。我相信你能使它们适应你的需要

public class TestDocumentFilter01 {

    public static void main(String[] args) {
        new TestDocumentFilter01();
    }

    public TestDocumentFilter01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(createField("1234567890"), gbc);
            add(createField("stackoverflow"), gbc);
            add(createField("abcdefghijklmnopqrstuvwxyz "), gbc);
        }

        protected JTextField createField(String mask) {
            JTextField field = new JTextField(10);
            MaskFilter df = new MaskFilter();
            df.setMask(mask);
            ((AbstractDocument) (field.getDocument())).setDocumentFilter(df);
            return field;
        }

    }

    public class MaskFilter extends DocumentFilter {

        private char[] maskSet;
        private String mask;

        public void setMask(String mask) {
            this.mask = mask;
            this.maskSet = mask.toCharArray();
            Arrays.sort(this.maskSet);
        }

        public String getMask() {
            return mask;
        }

        public void insertString(DocumentFilter.FilterBypass fb, int offset,
                                                         String string, AttributeSet attr)
                        throws BadLocationException {
            StringBuffer buffer = new StringBuffer(string);
            for (int i = buffer.length() - 1; i >= 0; i--) {
                char ch = buffer.charAt(i);
                if (Arrays.binarySearch(maskSet, ch) < 0) {
                    buffer.deleteCharAt(i);
                }
            }
            super.insertString(fb, offset, buffer.toString(), attr);
        }

        public void replace(DocumentFilter.FilterBypass fb,
                                                int offset, int length, String string, AttributeSet attr) throws BadLocationException {
            if (length > 0) {
                fb.remove(offset, length);
            }
            insertString(fb, offset, string, attr);
        }
    }
}

公共类TestDocumentFilter01{
公共静态void main(字符串[]args){
新的TestDocumentFilter01();
}
公共测试文档过滤器01(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
公共测试窗格(){
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridwidth=GridBagConstraints.rements;
添加(createField(“1234567890”),gbc;
添加(createField(“stackoverflow”),gbc;
添加(createField(“abcdefghijklmnopqrstuvxyz”),gbc;
}
受保护的JTextField createField(字符串掩码){
JTextField=新的JTextField(10);
MaskFilter df=新MaskFilter();
设置掩码(掩码);
((AbstractDocument)(field.getDocument()).setDocumentFilter(df);
返回字段;
}
}
公共类MaskFilter扩展了DocumentFilter{
私有字符[]掩码集;
私有字符串掩码;
公共void设置掩码(字符串掩码){
这个面具=面具;
this.maskSet=mask.toCharArray();
Arrays.sort(this.maskSet);
}
公共字符串getMask(){
返回掩码;
}
public void insertString(DocumentFilter.FilterBypass fb,int offset,
字符串字符串,属性集属性)
抛出BadLocationException{
StringBuffer=新的StringBuffer(字符串);
对于(int i=buffer.length()-1;i>=0;i--){
char ch=缓冲区charAt(i);
if(Arrays.binarySearch(maskSet,ch)<0){
缓冲区。删除字符(i);
}
}
super.insertString(fb,offset,buffer.toString(),attr);
}
公共无效替换(DocumentFilter.FilterBypass fb,
int offset、int length、String String、AttributeSet attr)引发BadLocationException{
如果(长度>0){
fb.移除(偏移、长度);
}
插入字符串(fb、偏移量、字符串、属性);
}
}
}

您有一个从hashmap中存储的字符创建过滤器的示例吗,该表包含允许的字符,@user2030181终于找到了时间:P