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.
Currently I have a method that uses a string buffer to write out a
Share
You could add a
javax.swing.text.Documentparameter to the method that writes to the string buffer. Then when you call the method, pass in the value ofJTextArea.getDocument()— this object represents the text in theJTextArea.Then each time you append text to the string buffer, also append it to the
Documentlike this:Note that this might make your existing string buffer redundant… effectively you’re using the
Documentobject 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.