So let's say hypothetically I have a JEditorPane.
I also have a void called addText(String S) which goes like this
public void addText(String s)
jeditorpane.setText(jeditorpane.getText() + s);
}
Now I will have a button and when pressed reads all the lines of a text file and writes them into the text editor box using a buffered reader and adding text line by line.
Why is it that when the button is pressed the application will freeze for a couple seconds then have everything posted on at once. Is their a way so you see everything posting dynamically and seeing itbeing posted one by one. That is how the code is written.
I have some sources from StackOverFlow that may help you:
Why is it that when the button is pressed the application will freeze for a couple seconds then have everything posted on at once.
You're doing slow reading on the Swing event thread, tying up the the thread and preventing it from doing its jobs including drawing to the GUI and interacting with the user.
Is their a way so you see everything posting dynamically and seeing itbeing posted one by one. That is how the code is written.
Yes, use a background thread, most easily obtained by using a SwingWorker. Read Concurrency in Swing.
You would likely want to use a generic SwingWorker, specifically SwingWorker<Void, String>
and use it's publish/process method pair to push the Strings out into your text component as each line is read.
Edit: I now see that both of the links that you placed in your question already mentioned this solution which makes me wonder why you asked this question.