0

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:

Community
  • 1
  • 1
Andre
  • 778
  • 1
  • 5
  • 23

1 Answers1

5

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Because they both didn't work for me – Andre Dec 24 '14 at 01:32
  • 2
    @Master: then that should be part of your question. You should show what you've tried, explain what is wrong or not working with your attempts. Else all you get is us regurgitating that which was explained before. Note that "didn't work" tells us little that we can use to help you move forward. Please be specific and detailed in your description. Consider updating your question. – Hovercraft Full Of Eels Dec 24 '14 at 01:36