I’m using a JTextPane (in a JScrollPane) as part of a custom logging system. (I needed multi-colour output so couldn’t use JTextArea.)
I have the logging part of it working, but I now need to be able to limit its content so that it doesn’t just grow in memory continuously.
There is no direct user input as all logs are system generated.
What I need to be able to do is to identify when the JTextPane had reached a specified number of lines, and then be able to delete the first line when the maximum is exceeded. This would allow me to keep a buffer of the last ‘x’ lines in the display.
How would I go about doing this?
Use DocumentFilter and check the Document’s length. Also you can use Document’s getText() method and count “\n” chars in the String.
Or you can override insertString() method of the document. If max possible amount of lines is achieved just call remove() and then super.insertString()