简体   繁体   中英

Tabs, newlines, DocumentFilters and eclipse text

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.

replace方法的方法的else部分中,仅替换“ \\ n”,而不替换“ \\ t”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM