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 ().