Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:36:52+00:00 2026-06-15T21:36:52+00:00

I’m trying to add functionality to a Swing JLabel and JTextArea such that: The

  • 0

I’m trying to add functionality to a Swing JLabel and JTextArea such that:

  • The user is only allowed to enter 500 characters into the textarea (max)
  • The label contains a string message telling the user how many characters they have left (after every key stroke or backspace)
  • When the components initialize the label reads “500 characters maximum!”
  • For the first 500 characters typed, for every keystroke (a – z, A – Z, 0 – 9, and punctuation) typed, the label reads “x characters remaining”, where x is the number of chars they have left before they reach the max of 500
  • When the 500th character is typed, the label reads “0 characters remaining”, and no further characters can be typed into the text area
  • If the user types the backspace button (KeyEvent.VK_BACK_SPACE), they “free” up a character, and the count increments. Thus if they had 400 characters remaining, and they type backspace, the label now reads “401 characters remaining”
  • If the user highlights a set of characters and performs a bulk command on them (such as a backspace, or replacing the highlighted text with a single character), the correct # of chars remaining will be calculated correctly and the label will be updated. So if they have 50 chars remaining, and they highlight 5 letters and hit backspace, they now have “55 characters remaining”

I have 90% of this functionality working, but have a few bugs, and have no clue as to how to implement the last item above (bulk commands on highlighted text). Here’s what I have:

boolean ignoreInput = false;
int charMax = 500;
JLabel charCntLabel = getLabel();
JTextArea myTextArea = getTextArea();

myTextArea.addKeyListener(new KeyListener() {
    @Override
    public void keyTyped(KeyEvent e) {
        return;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // If we should be ignoring input then set make sure we
        // enforce max character count and remove the newly typed key.
        if(ignoreInput)
            myTextArea.setText(myTextArea.getText().substring(0,
                myTextArea.getText().length()));
    }

    @Override
    public void keyPressed(KeyEvent e) {
        String charsRemaining = " characters remaining";
        int newLen = 0;

        // The key has just been pressed so Swing hasn't updated
        // the text area with the new KeyEvent.
        int currLen = myTextArea.getText().length();

        // Adjust newLen depending on whether the user just pressed
        // the backspace key or not.
        if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            newLen = currLen - 1;
            ignoreInput = false;
        }
        else
            newLen = currLen + 1;

        if(newLen < 0)
            newLen = 0;

        if(newLen == 0)
            charCntLabel.setText(charMax + " characters maximum!");
        else if(newLen >= 0 && newLen < charMax)
            charCntLabel.setText((charMax - newLen) + charsRemaining);
        else if(newLen >= charMax) {
            ignoreInput = true;
            charCntLabel.setText("0 " + charsRemaining);
        }
    }
});

The above code works pretty well, but has a few bugs:

  • It doesn’t prevent the user from typing in > 500 characters. When the user types in the 500th character, the label reads “0 characters remaining.” But you can continue to type in characters after that, and the label stays the same.
  • If you have > 500 characters in the textarea, and you start backspacing, you’ll see each character being removed from the textarea (the underlying model), but the label stays the same. But, once you backspace enough to get to the 500th character, and you backspace, the label will start changing properly, telling you that you have “1 characters remaining”, “2 characters remaining”, etc. So…
  • This code seems to work but just stops working > 500 characters. Once you get back inside that 500 char max, it begins working again.

The questions

  1. Is there a simpler way to implement this desired functionality (and for a Swing JTextArea)? I feel like I’m reinventing the wheel here and that there might be a “cleaner” way of enforcing character maximums and updating their respective labels.
  2. If not, can anybody spot my > 500 char bug? I’ve been looking at it all morning and am pulling my hair out.
  3. Most importantly, how do I implement my requirement to handle bulk commands to highlighted text? How do I hand text selections inside the textarea, listen for changes to the highlighted text (e.g., deleting multiple highlighted characters with the backspace button, etc.), and correctly calculate the new value for chars remaining?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:36:53+00:00Added an answer on June 15, 2026 at 9:36 pm

    You can limit the max size by using a DocumentFilter, check this documentation section, it has a working example of what you need.

    Take this as an example, I used the component from the example file above:

    import java.awt.BorderLayout;
    
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    import components.DocumentSizeFilter;
    
    public class Test {
    
        public static void main(String[] args) {
            new TestFrame().setVisible(true);
        }
    
        private static class TestFrame extends JFrame{
            private JTextField textField;
            private DefaultStyledDocument doc;
            private JLabel remaningLabel = new JLabel();
    
            public TestFrame() {
                setLayout(new BorderLayout());
    
                textField = new JTextField();
                doc = new DefaultStyledDocument();
                doc.setDocumentFilter(new DocumentSizeFilter(500));
                doc.addDocumentListener(new DocumentListener(){
                    @Override
                    public void changedUpdate(DocumentEvent e) { updateCount();}
                    @Override
                    public void insertUpdate(DocumentEvent e) { updateCount();}
                    @Override
                    public void removeUpdate(DocumentEvent e) { updateCount();}
                });
                textField.setDocument(doc);
    
                updateCount();
    
                add(textField, BorderLayout.CENTER);
                add(remaningLabel, BorderLayout.SOUTH);
    
                setLocationRelativeTo(null);
                pack();
            }
    
            private void updateCount()
            {
                remaningLabel.setText((500 -doc.getLength()) + " characters remaining");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.