Currently I have a method that uses a string buffer to write out a string as it runs. The method then returns this string which I then make appear in a JTextArea
. Is there another way that I can do this so that as the method runs the string gets written to the text area instead of once it has finished. Thanks.
You could add a javax.swing.text.Document
parameter to the method that writes to the string buffer. Then when you call the method, pass in the value of JTextArea.getDocument()
— this object represents the text in the JTextArea
.
Then each time you append text to the string buffer, also append it to the Document
like this:
document.insertString(document.getLength(), stringToAppend, null);
Note that this might make your existing string buffer redundant... effectively you're using the Document
object as a string buffer instead. Note also that you can bet this technique will be much, much slower than writing out the string buffer in full before displaying it.