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

  • SEARCH
  • Home
  • 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 9242847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:40:23+00:00 2026-06-18T08:40:23+00:00

So, I have an additional issue regarding THIS question. Once I apply the fix,

  • 0

So, I have an additional issue regarding THIS question.

Once I apply the fix, go to notepad, typeout bunch of tabs and newlines with some random characters and then paste them into my program, it all works peachy.

However, being the closest text with bunch of tabs and newlines, I tried pasting a part the code itself to the JTextArea. All tabs and newlines stuck there and weren’t filtered out.

Although my users probably won’t paste eclipse code into my program , I can’t be sure that eclipse code is the only exception. So I’d like to know why is this happening.

Also, I’d like for my code to filter out the blank characters except for space caracter and turn them into space character. I think tab and newline are the only ones, but if there are any more, please let me know.

Anyway, what do I have to change to make it work?

Here’s the fixed SSCCE:

package core;

import java.awt.BorderLayout; import java.awt.Dimension; import java.io.FileNotFoundException; import java.io.IOException;

import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter;

class DefaultDocFilter extends DocumentFilter {
    public void insertString(FilterBypass fb, int offs,String str, AttributeSet a) throws BadLocationException 
    {
        if ((fb.getDocument().getLength() + str.length()) <= 2000)
        {
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");
            fb.insertString(offs, str, a);
        }
        else
        {
            int spaceLeft = 2000 - fb.getDocument().getLength();
            if (spaceLeft <= 0)
                return;

            str = str.substring(0, spaceLeft);
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");

            fb.insertString(offs, str, a);
        }
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException 
    {
        if (str.equals("\n") || str.equals("\t"))
        { 
            str = "";
        }
        if ((fb.getDocument().getLength() + str.length() - length) <= 2000)
        {
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");
            fb.replace(offs, length, str, a);
        }
        else
        {
            int spaceLeft = 2000 - fb.getDocument().getLength() + length;
            if (spaceLeft <= 0)
                return;

            fb.replace(offs, length, str.substring(0,spaceLeft).replaceAll("\n", " "), a);
        }
    } }


public class Main {
    public static JFrame mWindow;

    public static void main(String[] args) throws FileNotFoundException, IOException
    {   
        //create main window
        mWindow = new JFrame("title");
        mWindow.setSize(1000, 800);
        mWindow.setMinimumSize(new Dimension(1000, 800));
        mWindow.setLocationRelativeTo(null);
        mWindow.setLayout(new BorderLayout());
        mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea a = new JTextArea();
        AbstractDocument doc = (AbstractDocument) a.getDocument();
        doc.setDocumentFilter(new DefaultDocFilter());
        a.setLineWrap(true);
        a.setWrapStyleWord(true);

        mWindow.add(a);
        mWindow.pack();

        mWindow.setVisible(true);

        mWindow.repaint();
        mWindow.validate();
    } }

It’s Java 1.7. Create a new project, package core, file Main.

Document filter is the first class and it’s applied to the JTextArea you’ll see. Everything you need is within that class.

EDIT: I fixed the SSCCE. Also, the problem only occurs when you try to paste more character that can fit the JTextArea (I set limit to 2000). Then tas and newlines wont get filtered out.

  • 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-18T08:40:24+00:00Added an answer on June 18, 2026 at 8:40 am

    In the replace method, in the else part of the method, you only replace “\n” but not “\t”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an additional question regarding sub categories. The current problem is that when
I am working only in C regarding this issue. I have two function prototypes
This question is regarding the select_list in watir. We have a web application which
I have a flash player that loads in some additional features from flash files
We have subclassed the Silverlight Application class to add some additional functionality and then
I have a FrameLayout view which contains one (MapView-like) control and some additional buttons
I have a question regarding copying cells from one sheet to another using VBA.
OK, so I've read something like 10 other threads here regarding this infamous issue,
This is a Question regarding the topic of: Contributing to Open Source . Given
I am working on an NHibernate project and have a question regarding updating transient

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.