just check the contents and wipe it with the corresponding maximum buffer size .. since it is a JTextPane , you will work on the document class used by the text panel:
void clampBuffer(int incomingDataSize) { Document doc = textPane.getStyledDocument(); int overLength = doc.getLength() + incomingDataSize - BUFFER_SIZE; if (overLength > 0) { doc.remove(0, over_length); } }
This is just a fragment that I wrote, did not personally verify it. It is just to give you this idea. Of course, it must be started before adding text to the text panel.
Btw, if you are not using the features of the rich JTextPane editor, I suggest you use JTextArea , which is much more personal.
Jack
source share