How does Java swing refresh jtextarea in real time to display the added content?
Run textarea in the code. after append ("message"), if you want this update to be immediately displayed on the Interface rather than Refresh after the swing main thread returns, we usually call textarea after this statement. invalidate () and textarea. repaint (). The problem is that this method does not have any effect, and the content of textarea does not change. This may be a bug in swing. There is a clumsy way to achieve this effect, that is, to execute the following statement textarea. paintimmediately (textarea. getbounds ());
Or
Textarea. paintimmediately (textarea. getx (), textarea. Gety (), textarea. getwidth (), textarea. getheight ());
At this time, you will find that the messages you just added have been displayed in real time.
Saga_java 2012-06-07 on the first floor
Are you sure textarea. append ("message") is running on the swing event processing thread?
SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {textArea.append("message");}});
On the second floor, Zhou Fan Yang 2012-06-07
It should be on the frame window thread, because it takes a long time to process data after I click the button. At this time, the button is always displayed as the pattern to be clicked, the frame thread is blocked, so textarea. append ("message"); after running, the text box is not displayed in real time! Another method is to start another thread when processing data. No matter how long it takes to process the data, textarea. append ("message"); after running, the text box will be displayed in real time!
Saga_java 2012-06-07 on the third floor
Time-consuming operations should not be performed on the swing event processing thread, but only one other thread
Instance
When other external classes update and display jtextarea in real time, you must first set jtextarea to public static. For example, if a frame with the main window has a jtextarea, update the data obtained by the data to the jtextarea of the frame in real time. Then:
Assume that the frame class of the main window is:
Class frame extends jframe implements ...... {// Set jtextarea public static jtextarea ;...... Public frame () {// constructor jtextarea = new jttextarea ();...... Showdata (); // assume that you need to obtain and display data when creating the main form ...... } Public void showdata () {New thread (New runnable () {// start data acquisition, update display process public void run () {data d = new data (); D. outdata ();}}). start ();//}...... }
Assume that the data type data is:
Class data {...... Public Data (){...... }...... // Assume that the data is an auto increment and the Public void outdata () {for (INT I = 0; I <10000; I ++) frame is continuously output. jtextarea. append ("current data:" + I + "\ n"); // call the jtextarea in the main window to add data. The try {thread is displayed. currentthread (). sleep (100); // enables the current process to sleep for several milliseconds to display the dynamic update effect. Of course, this will take time} catch (interruptedexception ex) {// capture interrupt exceptions }}...... }