Home > Back-end >  Added to the JScrollPane JTextArea unable to display the scroll bar
Added to the JScrollPane JTextArea unable to display the scroll bar

Time:11-29

Your bosses, I meet a problem, several days not solve, to seek the help of,
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:

@ 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.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 method
This 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:

reference 4 floor sunyiz response:
unable to real-time display should be related to sleep in your updateThread method
This 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
well, yes, I'm in an ActionEvent listener this update_Thread, then use the button to add the listener, the mean of I make the listener's thread to sleep, affect the UI map?

CodePudding user response:

reference 5 floor Hudson lu ze reply:
well, yes, I'm in an ActionEvent listener this update_Thread, then use the button to add the listener, the mean of I make the listener's thread to sleep, affect the UI map?


Well, is this meaning,
Before you can make words refresh, paintImmediately because you forced to call the text control, but is not appropriate to do so

CodePudding user response:

refer to 6th floor sunyiz response:
Quote: refer to fifth floor Hudson lu ze response:

Well, yes, I'm in an ActionEvent listener this update_Thread, then use the button to add the listener, the mean of I make the listener's thread to sleep, affect the UI map?


Well, is this meaning,
Before you can make words refresh, because you are forced to call the paintImmediately text control, but this is not the right
well, try the next can get rid of all sleep inside the listener can be normal print, scroll bars can be real-time display, but because the listener involves network data read inside, and I don't know can read the number of bytes, I now realize is design waiting time to read, what to do if you don't sleep can use this delay does not affect the UI map? Bosses,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related