Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextPane inserting large text problem

I've a problem: firstly I load some large text from file. After that I want to display it in my JTextPane. For inserting text into JTextPane I use:

 SwingUtilities.invokeLater(new Runnable()
 {

 public void run()
 {
   textPane.setText(someLargeString);
  } 
});

But while my text inserts into JTextPane all UI is frozen.

Is there any ability to insert large String into JTextPane but without UI freezing?

Thanks

P.S. Process of loading data is in another thread. But after data is loaded I need to put it into JTextPane. So I'm invoking setting text in swing. But my UI freeze. Why?

like image 540
stemm Avatar asked Jun 12 '26 12:06

stemm


2 Answers

You should not call setText on the Swing EDT; setText is thread-safe, see the API. setText has more to do with the underlying AbstractDocument than with Swing. setText obtains a lock on the document before modifying it.

However getText must be called from the Swing EDT.

like image 127
toto2 Avatar answered Jun 15 '26 03:06

toto2


May be some of the tips could help as well http://java-sl.com/JEditorPanePerformance.html

trashgod's answer is correct if you need just plain text. If you have text with styles you need the JEditorPane/JTextPane.

like image 36
StanislavL Avatar answered Jun 15 '26 03:06

StanislavL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!