将限制整数设置为JTextField

时间:2013-03-26 18:06:22

标签: java swing jlabel jtextfield setbounds

如何为用户设置输入限制,我的意思是用户只能输入2或4个整数而不能再输入。

代码:

 JLabel dateD = new JLabel("| Date  Day:");
 dateD.setBounds(170,270, 120, 25);

 dateDD = new JTextField();
 dateDD.setBounds(235,270, 20, 25);

2 个答案:

答案 0 :(得分:1)

使用DocumentFilter应该排除你,按如下方式创建过滤器: -

class MaximumCharacters extends DocumentFilter {

        private int maxLength;

        public MaximumCharacters() {
            maxLength = 10; // The number of characters allowed
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String string,
                AttributeSet attr) throws BadLocationException {
            if (maxLength > 0
                    && fb.getDocument().getLength() + string.length() <= maxLength) {
                super.insertString(fb, offset, string, attr);
            }
        }

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

            if ((fb.getDocument().getLength() + text.length() - length) <= maxLength)
                super.replace(fb, offset, length, text, attrs);
        }
    }

然后将DocumentFilter设置为JTextField组件,如下所示: -

((AbstractDocument) dateDD.getDocument()).setDocumentFilter(new MaximumCharacters());

答案 1 :(得分:0)

对于上面的答案,下面是我为处理上述情况而创建的效用方法 -

    /**
     * Method installNumericMaximumCharacters.
     * 
     * @param document
     *            AbstractDocument
     * @param numberofChars
     *            int
     */
    public static void installNumericMaximumCharacters(
            AbstractDocument document, final int numberofChars) {
        document.setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset,
                    String string, AttributeSet attr)
                    throws BadLocationException {
                try {
                    if (string.equals(".")
                            && !fb.getDocument()
                                    .getText(0, fb.getDocument().getLength())
                                    .contains(".")) {
                        super.insertString(fb, offset, string, attr);
                        return;
                    }
                    Double.parseDouble(string);
                    if (fb.getDocument().getLength() + string.length() <= numberofChars) {
                        super.insertString(fb, offset, string, attr);
                    } else {
                        Toolkit.getDefaultToolkit().beep();
                    }
                } catch (Exception e) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length,
                    String text, AttributeSet attrs)
                    throws BadLocationException {
                try {
                    if (text.equals(".")
                            && !fb.getDocument()
                                    .getText(0, fb.getDocument().getLength())
                                    .contains(".")) {
                        super.insertString(fb, offset, text, attrs);
                        return;
                    }
                    Double.parseDouble(text);
                    int l = fb.getDocument().getLength() - length
                            + text.length();
                    if (length > 0) {
                        fb.getDocument().remove(offset, length);
                    }
                    if (l <= numberofChars) {
                        super.insertString(fb, offset, text, attrs);

                    } else {
                        Toolkit.getDefaultToolkit().beep();
                    }
                } catch (Exception e) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        });
    }

该方法有点自我解释。当Document发生变化时,它会唤起DocumentFilter。请注意,setText() Document不能FilterBypass。您必须使用{{1}}对象。