

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to restrict the number of digits inside JPasswordField in Java?
A JPasswordField is a subclass of JTextField and each character entered in a JPasswordField can be replaced by an echo character. This allows confidential input for passwords. The important methods of JPasswordField are getPassword(), getText(), getAccessibleContext() and etc. By default, we can enter any number of digits inside JPasswordField. If we want to restrict the digits entered by a user by implementing a DocumentFilter class and need to override the replace() method.
Syntax
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
Example
import java.awt.*; import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class JPasswordFieldDigitLimitTest extends JFrame { private JPasswordField passwordField; private JPanel panel; public JPasswordFieldDigitLimitTest() { panel = new JPanel(); ((FlowLayout) panel.getLayout()).setHgap(2); panel.add(new JLabel("Enter Pin: ")); 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); } } }); panel.add(passwordField); add(panel); setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JPasswordFieldDigitLimitTest(); } }
Output
- Related Questions & Answers
- How to set Echo Char for JPasswordField in Java?
- How can we show/hide the echo character of a JPasswordField in Java?
- Number of digits in the nth number made of given four digits in C++
- Java program to Count the number of digits in a given integer
- How can we limit the number of characters inside a JTextField in Java?
- How to find the number of digits in a given number using Python?
- Java Program to Count Number of Digits in an Integer
- Find the Largest number with given number of digits and sum of digits in C++
- Golang Program to Count the Number of Digits in a Number
- How to count digits of given number? JavaScript
- How to restrict dynamic allocation of objects in C++?
- Number of digits that divide the complete number in JavaScript
- Display two digits day number in Java
- Find smallest number with given number of digits and sum of digits in C++
- Java Program to Find Sum of Digits of a Number using Recursion
Advertisements