Note: According to the suggestions of yigemaser, jfml, and crazyjavar, I would like to express my gratitude for the help of the three parties!
When writing a UI application, it is usually in some event processing processes, especially when the processing is time-consuming, and it is hoped that some progress information will be displayed to the user in a timely manner. Generally, a text control is used to display the progress information. For example, in the following program, there is a jtextpane and jbutton, which requires some time-consuming processing in the Action event of jbutton. In the example program, thread is used. sleep () sleep the current thread for 3 seconds to simulate time-consuming operations. Action event processing is divided into three steps. We hope to display the current progress on jtextpane in time.
The Code is as follows:
- PackageBruce. test;
- ImportJavax. Swing .*;
- ImportJava. AWT.Container;
- ImportJava. AWT.Borderlayout;
- ImportJava. AWT.Dimension;
- ImportJava. AWT. event.Windowadapter;
- ImportJava. AWT. event.Actionlistener;
- ImportJava. AWT. event.Actionevent;
- /**
- * UI refreshing during event processing
- * @ Author Bruce
- * @ Version 1.0
- */
- Public classTestuiupdate2 {
- PublicTestuiupdate2 (){
- Testuiupdate2frame frame =NewTestuiupdate2frame ();
- Frame. Pack ();
- Frame. setvisible (True);
- }
- Public static voidMain (String[] ARGs ){
- NewTestuiupdate2 ();
- }
- }
- ClassTestuiupdate2frameExtendsJframe{
- JtextpanePane =NewJtextpane();
- JbuttonButton =NewJbutton("Action ...");
- Testuiupdate2frame (){
- Init ();
- This. Setdefaclocloseoperation (Jframe. Exit_on_close );
- Button. addactionlistener (NewActionlistener(){
- Public voidActionreceivmed (ActioneventE ){
- Try{
- Pane. settext ("Step one ...");
- Thread. Sleep (3000 );
- Pane. settext ("\ nstep two ...");
- Thread. Sleep (3000 );
- Pane. settext ("\ nfinished .");
- Thread. Sleep (3000 );
- }
- Catch(InterruptedexceptionIE ){
- // Ignored
- }
- }
- });
- }
- Private voidInit (){
- Pane. setpreferredsize (NewDimension(300,200 ));
- ContainerContent = getcontentpane ();
- Content. setlayout (NewBorderlayout());
- Content. Add (PANE,Borderlayout. Center );
- Content. Add (button,Borderlayout. South );
- }
- }
However, during actual operation, we can find that after clicking jbutton, jtextpane cannot be updated in time, but the final information can be displayed only after the action event of the entire jbutton is processed. Why is this happening? Although jtextpane is updated during jbutton Action event processing, jtextpane is run in the current main thread. Although jtextpane updates the content, however, there is no execution opportunity to refresh the display.
The solution to this problem is very simple. You only need to put the jbutton action processing code into a new thread and then start this thread. In addition, most swing operations are non-thread-safe, so refreshing the swing interface is also put in a separate thread and calls swingutilities. invokelater () for execution. In this way, the interface for processing the Action event, updating the jtextpane, and the main thread run in their respective threads, respectively, can be executed in a timely manner. Jbutton
- The processing code of actionreceivmed (actionevent E) is modified as follows:
- [Code] button. addactionlistener (NewActionlistener (){
- Public voidActionreceivmed (actionevent e ){
- Try
- {
- NewThread(){
- Public voidRun (){
- Try{
- Showmessage ("Step one ...");
- Thread. Sleep (3000 );
- Showmessage ("\ nstep two ...");
- Thread. Sleep (3000 );
- Showmessage ("\ nfinished .");
- Thread. Sleep (3000 );
- }
- Catch(InterruptedexceptionIE ){
- // Ignored
- }
- }
- }. Start ();
- }
- Catch(ExceptionEx)
- {
- Ex. printstacktrace ();
- }
- }
- });
The showmessage method is as follows:
- Private voidShowmessage (FinalStringMSG ){
- Swingutilities. invokelater (NewRunnable(){
- Public voidRun (){
- Pane. settext (pane. gettext () + MSG );
- }
- });
- }
You can test the running observation results. This also makes the interface more friendly, because if you do not put the action processing code in a separate thread, after you click jbutton, the interface stops all responses until the action processing code is executed. You can extend this method to allow users to stop time-consuming operations at any time, making the interface more friendly.