Ui refresh in jbutton event processing

Source: Internet
Author: User

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:

  1. PackageBruce. test;
  2. ImportJavax. Swing .*;
  3. ImportJava. AWT.Container;
  4. ImportJava. AWT.Borderlayout;
  5. ImportJava. AWT.Dimension;
  6. ImportJava. AWT. event.Windowadapter;
  7. ImportJava. AWT. event.Actionlistener;
  8. ImportJava. AWT. event.Actionevent;
  9. /**
  10. * UI refreshing during event processing
  11. * @ Author Bruce
  12. * @ Version 1.0
  13. */
  14. Public classTestuiupdate2 {
  15. PublicTestuiupdate2 (){
  16. Testuiupdate2frame frame =NewTestuiupdate2frame ();
  17. Frame. Pack ();
  18. Frame. setvisible (True);
  19. }
  20. Public static voidMain (String[] ARGs ){
  21. NewTestuiupdate2 ();
  22. }
  23. }
  24. ClassTestuiupdate2frameExtendsJframe{
  25. JtextpanePane =NewJtextpane();
  26. JbuttonButton =NewJbutton("Action ...");
  27. Testuiupdate2frame (){
  28. Init ();
  29. This. Setdefaclocloseoperation (Jframe. Exit_on_close );
  30. Button. addactionlistener (NewActionlistener(){
  31. Public voidActionreceivmed (ActioneventE ){
  32. Try{
  33. Pane. settext ("Step one ...");
  34. Thread. Sleep (3000 );
  35. Pane. settext ("\ nstep two ...");
  36. Thread. Sleep (3000 );
  37. Pane. settext ("\ nfinished .");
  38. Thread. Sleep (3000 );
  39. }
  40. Catch(InterruptedexceptionIE ){
  41. // Ignored
  42. }
  43. }
  44. });
  45. }
  46. Private voidInit (){
  47. Pane. setpreferredsize (NewDimension(300,200 ));
  48. ContainerContent = getcontentpane ();
  49. Content. setlayout (NewBorderlayout());
  50. Content. Add (PANE,Borderlayout. Center );
  51. Content. Add (button,Borderlayout. South );
  52. }
  53. }

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

  1. The processing code of actionreceivmed (actionevent E) is modified as follows:
  2. [Code] button. addactionlistener (NewActionlistener (){
  3. Public voidActionreceivmed (actionevent e ){
  4. Try
  5. {
  6. NewThread(){
  7. Public voidRun (){
  8. Try{
  9. Showmessage ("Step one ...");
  10. Thread. Sleep (3000 );
  11. Showmessage ("\ nstep two ...");
  12. Thread. Sleep (3000 );
  13. Showmessage ("\ nfinished .");
  14. Thread. Sleep (3000 );
  15. }
  16. Catch(InterruptedexceptionIE ){
  17. // Ignored
  18. }
  19. }
  20. }. Start ();
  21. }
  22. Catch(ExceptionEx)
  23. {
  24. Ex. printstacktrace ();
  25. }
  26. }
  27. });

The showmessage method is as follows:

  1. Private voidShowmessage (FinalStringMSG ){
  2. Swingutilities. invokelater (NewRunnable(){
  3. Public voidRun (){
  4. Pane. settext (pane. gettext () + MSG );
  5. }
  6. });
  7. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.