I have a text field in my application. Despite it being a text field, users sometimes paste huge amounts of text into it. Additionally, other functions of the problem set large amounts in as well.
Sometimes there is so much text that the JVM gets an access violation in fontmanager.dll. Oracle doesn't appear to be interested in fixing the problem itself, so I would like to at least try to avoid it.
Limiting the amount of text the user inputs is apparently not acceptable (otherwise this would be the most obvious solution) but it's acceptable to allow it to be set and then disable the text field. When the text is bound back to the model, it should contain the full text again.
Since this is inherently a bug in the view, I figured that the fix should be in the view, as opposed to working around it in the model and adding the 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 does pass naive unit tests, but once I wrote an additional test for BeansBinding, I found that it didn't work because BeansBinding doesn't bind to the text property but rather binds to the Document, simulating a text property. So actually getText() always returns an empty string on that test.
I am now looking at trying to make a Document implementation which will do what I want, but it sure isn't easy to do this kind of trick at the document level. I can see all the methods it has, but I can't find a good way to limit the text without also making that text unavailable when calling getText().