How to limit the number of digits in a JPasswordField in Java?

后端 未结 4 786
轻奢々
轻奢々 2020-12-12 01:20

I have my Java code working in Eclipse but I need to add a few functionnality.

First, how to limit the number of digits that can be entered by the user ? Actually I

4条回答
  •  心在旅途
    2020-12-12 01:49

    How can I play with the size of the JPassword box ? Is there a way to modify it just like a JTextField for example ? Because my line "p1.setPreferredSize(new Dimension(100, 25));" does not seem to really let me modify the size of the box.

    Don't use setPrefferedSize() instead use this constructor JPasswordField(int col) .Read more about that here Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

    How to limit the number of digits that can be entered by the user ? Actually I have a JPasswordField that let a person enter a pin Code, and I would like this JPasswordField limited to 4 digits maximum. So how to stop the input as soon as 4 digits are entered ?

    For limit input you can use DocumentFilter as shown in example below.

     public class JPasswordFieldTest {
    
        private JPanel panel;
    
        public JPasswordFieldTest() {
            panel = new JPanel();
            //set horizontal gap
            ((FlowLayout) panel.getLayout()).setHgap(2);
    
            panel.add(new JLabel("Enter pin :"));
            JPasswordField passwordField = new JPasswordField(4);
            PlainDocument document = (PlainDocument) passwordField.getDocument();
            document.setDocumentFilter(new DocumentFilter() {
    
                @Override
                public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                    String string = fb.getDocument().getText(0, fb.getDocument().getLength()) + text;
    
                    if (string.length() <= 4) {
                        super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
                    }
                }
    
            });
            panel.add(passwordField);
            JButton button = new JButton("OK");
            panel.add(button);
    
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI("Password Example");
                }
            });
    
        }
    
        private static void createAndShowGUI(String str) {
            JFrame frame = new JFrame(str);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            JPasswordFieldTest test = new JPasswordFieldTest();
            frame.add(test.panel);
            frame.pack();
            frame.setVisible(true);
        }
    
    }
    

    enter image description here

提交回复
热议问题