Can I limit the length of the text in a JTextField that can be colored while preserving the full text?

I have a text box in my application. Despite the fact that this is a text field, users sometimes insert a huge amount of text into it. In addition, other functions of the problem also set large amounts.

Sometimes there is so much text that the JVM gets an access violation in fontmanager.dll. Oracle does not seem to be interested in fixing the problem itself, so I would like to at least try to avoid it.

Limiting the number of user text inputs is apparently unacceptable (otherwise it would be the most obvious solution), but it is acceptable to allow its installation, and then turn off the text field. When the text is anchored to the model, it should contain the full text again.

Since this is essentially a mistake in the view, I decided that the fix should be in the view, and not work around it in the model and add additional properties there.

My first attempt went something like this:

public class LimitedTextField extends JTextField {
    static final int LIMIT = 10000;
    private String fullString;

    @Override
    public void setText(String text) {
        if (text != null && text.length() > LIMIT) {
            fullString = text;
            setEnabled(false);
        } else {
            fullString = null;
            super.setText(text);
            setEnabled(true);
        }
    }

    @Override
    public String getText() {
        if (fullString != null) {
            return fullString;
        } else {
            return super.getText();
        }
    }
}

This passes naive unit tests, but as soon as I wrote an additional test for BeansBinding, I found that it does not work, because BeansBinding does not bind to the text property, but rather binds to the Document, simulating the text property. So actually getText () always returns an empty string in this test.

Now I am trying to make an implementation of a document that will do what I want, but of course it is not easy to do such a trick at the document level. I see all the methods that it has, but I can’t find a good way to limit the text without making this text inaccessible when calling getText ().

+5
3

. , , . , , (. http://forums.sun.com/thread.jspa?threadID=481290), , , . UIDelegate , . . , .

+1

JTextField, , sinlge JTextArea. . , , .

, , getText(), . , , .

+1

Limited Length . , . ( ):

package com.twist.ui.text.document;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class LimitedLengthDocument extends PlainDocument {
    private static final long serialVersionUID = 1L;

    private int limit;

    public LimitedLengthDocument(int limit) {
        super();
        this.limit = limit;
    }

    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
        if (str == null)
            return;

        // insert the string as usual.
        super.insertString(offset, str, attr);

        // If user tries to paste in a String that will not fit into the textfield, this approach will 
        // insert the text and remove the extra characters from the right.      

        // if resultant doc length is greater than the allowable size, truncate the document.
        if( getLength() > limit  )
            super.remove(limit, getLength() - limit);
    }
}
+1

All Articles