JTextField limiting character amount input and accepting numeric only

前端 未结 7 1225

here\'s the code that i have on how to limit the character input length

class JTextFieldLimit extends PlainDocument {
  private int limit;
  // optional uppe         


        
相关标签:
7条回答
  • 2020-11-28 00:21
    import java.awt.Component;
    import java.awt.KeyboardFocusManager;
    import java.awt.event.KeyEvent;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.JTextComponent;
    import org.apache.commons.lang.StringUtils;
    
    public class NumberTextField extends JTextField {
    
        protected int maxlength = 0;
    
        public NumberTextField() {
            this(10);
        }
    
        public NumberTextField(int length) {
            super();
            this.maxlength = length;
            initializeForNumbers();
        }
    
        public void setMaxLength(int length) {
            this.maxlength = length;
        }
    
        protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_ESCAPE) {
                return false;
            }
            return super.processKeyBinding(ks, e, condition, pressed);
        }
    
        private void initializeForNumbers() {
            Document document = getDocument();
            if (document != null) {
                ((AbstractDocument) document).setDocumentFilter(new DocumentHandler());
            }
        }
    
        private class DocumentHandler extends DocumentFilter {
    
            public void insertString(DocumentFilter.FilterBypass fb,
                    int offset, String string, AttributeSet attr)
                    throws BadLocationException {
                if (string == null) {
                    return;
                } else {
                    replace(fb, offset, 0, string, attr);
                }
    
            }
    
            public void remove(DocumentFilter.FilterBypass fb,
                    int offset, int length)
                    throws BadLocationException {
    
                replace(fb, offset, length, "", null);
            }
    
            public void replace(final DocumentFilter.FilterBypass fb,
                    int offset, int length, String text, AttributeSet attrs)
                    throws BadLocationException {
    
                Document doc = fb.getDocument();
                int currentLength = doc.getLength();
                String currentContent = doc.getText(0, currentLength);
                String before = currentContent.substring(0, offset);
                String after = currentContent.substring(length + offset, currentLength);
                String newValue = before + (text == null ? "" : text) + after;
                if (newValue.length() > maxlength) {
                    return;
                } else {
                    checkInput(newValue, offset);
                    fb.replace(offset, length, text, attrs);
    
                    if (doc.getLength() >= maxlength) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                                if (c != null && c instanceof JTextComponent) {
                                    JTextComponent component = (JTextComponent) c;
                                    Document compDoc = component.getDocument();
                                    if (compDoc.equals(fb.getDocument())) {
                                        KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
                                    }
                                }
                            }
                        });
                        return;
                    }
                }
            }
    
            private void checkInput(String proposedValue, int offset)
                    throws BadLocationException {
                if (proposedValue.length() > 0 && !StringUtils.isNumeric(proposedValue)) {
                    throw new BadLocationException(
                            proposedValue, offset);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题