Real-time implementation requirements: as long as a new String to print to JTextArea (JTextArea added to the JScrollPane), and real-time display the scroll bar,
I realize:, every time a new String will start a new thread (Thread_update) for printing, part of the code is as follows,
Problem: the JScrollPane only in the end of a scroll bar shows that intermediate String than JScrollPane visible range but failed to show a progress bar,
Seek help from bosses,
//initialize JTextArea () and the JScrollPane ()
Return_text=new JTextArea ();
//return the global output edit box cannot edit
Return_text. SetEditable (false);
//set up automatic newline
Return_text. SetLineWrap (true);
//set the line breaks constantly words
Return_text. SetWrapStyleWord (true);
//return output interface initialization (with a progress bar)
Return_scrollpane=new JScrollPane ();
Return_scrollpane. SetViewportView (return_text);
Return_scrollpane. SetAutoscrolls (true);
Return_scrollpane. SetBounds (10, 40, 570, 408);
The container. The add (return_scrollpane);
setVisible(true);
SetDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
//each time produce STH is about to start a new thread for printing
UpdateThread (STH);
//start print thread
Private void updateThread (String STH) {
Thread_update Thread_update=new Thread_update (STH);
Thread_update. Start ();
Try {
Thread.sleep (1000);
} the catch (Exception e) {
e.printStackTrace();
}
}
//the child thread implementation
Private class Thread_update extends Thread {
Private String STH;
Public Thread_update (String STH) {
This. STH=STH;
}
@ Override
Public void the run () {
Return_text. SetText (return_text. GetText () + STH);
Return_text. PaintImmediately (return_text getBounds ());
Return_text. SetCaretPosition (return_text. GetDocument () getLength ());
Return_scrollpane. Revalidate ();
Try {
Thread.sleep (100);
} the catch (Exception e) {
e.printStackTrace();
}
}
}
CodePudding user response:
Put your thread to try like this:
//the child thread implementation
Private static class Thread_update extends Thread {
Private String STH;
Public Thread_update (String STH) {
This. STH=STH;
}
@ Override
Public void the run () {
SwingUtilities. InvokeLater (new Runnable () {
@ Override
Public void the run () {
Return_text. Append (STH);
}
});
Try {
Thread.sleep (100);
} the catch (Exception e) {
e.printStackTrace();
}
}
}
CodePudding user response:
@ OverridePublic void the run () {
//return_text setText (return_text getText () + STH);
//return_text paintImmediately (return_text getBounds ());
//return_text setCaretPosition (return_text getDocument () getLength ());
//return_scrollpane revalidate ();
//try {
//Thread. Sleep (100);
//} the catch (Exception e) {
//e.p rintStackTrace ();
//}
SwingUtilities. InvokeLater (new Runnable () {
@ Override
Public void the run () {
Return_text. Append (STH);
}
});
Try {
Thread.sleep (100);
} the catch (Exception e) {
e.printStackTrace();
}
}
}
Bosses, the run () copy your JTextArea now are not real-time shows the
CodePudding user response:
CodePudding user response:
Unable to real-time display should be related to sleep in your updateThread methodThis way, you should be a Listener invokes the,
The listener Swing all controls are performed for EDT
The EDT is responsible for the UI map
You sleep in the EDT, impede normal refresh interface
CodePudding user response: