
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Limit Number of Characters in JTextField in Java
In this article, we will learn how to limit the number of characters inside a JTextField in Java. Whether you're creating a form for phone numbers, zip codes, or usernames, controlling the maximum number of characters is important for data validation.
JTextField
A JTextField is one of the most important Swing components that allows the user to an input text value in a single line format. We can restrict the number of characters that the user can enter into a JTextField can be achieved by using the PlainDocument class.
PlainDocument Class
PlainDocument is a subclass that extends the AbstractDocument, a plain document that maintains no character attributes. The default element structure for this document is a map of the lines in the text. It serves as the default document model for JTextField, JTextArea.
public class PlainDocument extends AbstractDocument
Limiting the Number of Characters Inside a JTextField
We can implement the logic by using a PlainDocument class, hence we can allow a user to enter a maximum of 10 characters, and it doesn't allow if we enter more than 10 characters.
The following are the steps for limiting the number of characters inside a JTextField in Java:
Declares a class named "JTextFieldLimit " that extends PlainDocument, and limit is a private field storing the maximum allowed characters.
class JTextFieldLimit extends PlainDocument { private int limit;
The super() method calls the parent PlainDocument constructor, and this.limit sets the maximum allowed characters.
JTextFieldLimit(int limit) { super(); this.limit = limit; }
First ignores null input to prevent errors, then checks if the current text length and the new text length sum is smaller than the limit value, and if it is true, then returns the current document length using the getLength() method.
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } }
Example
Below is an example of limiting the number of characters inside a JTextField in Java:
import java.awt.*; import javax.swing.*; import javax.swing.text.*; class JTextFieldLimit extends PlainDocument { private int limit; JTextFieldLimit(int limit) { super(); this.limit = limit; } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } } } public class JTextFieldLimitTest extends JFrame { JTextField textfield; JLabel label; public static void main(String[]args){ new JTextFieldLimitTest().GUI(); } public void GUI() { setLayout(new FlowLayout()); label = new JLabel("max 10 chars"); textfield = new JTextField(15); add(label); add(textfield); textfield.setDocument(new JTextFieldLimit(10)); setSize(350,300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }