java - Swing ProgressMonitor not working -


i trying learn progressmonitor in java swing. created simple test code -

public class progressmonitortest extends jframe  { private jpanel contentpane; private progressmonitor progressmonitor; private jbutton button; private static progressmonitortest frame; private static boolean isframeready;  public jbutton getbutton() {     return button; }  public progressmonitor getprogressmonitor() {     return progressmonitor; }  /**  * launch application.  */ public static void main(string[] args)  {     eventqueue.invokelater(new runnable()      {         public void run()          {             try              {                 frame = new progressmonitortest();                 frame.setvisible(true);                 isframeready = true;             }              catch (exception e)              {                 e.printstacktrace();             }         }     });      while(!isframeready)     {         //     }      frame.getbutton().addactionlistener(new actionlistener()      {            @override         public void actionperformed(actionevent e)          {             try             {                 for(int i=0;i<=10;i++)                 {                     final int percent = i;                     swingutilities.invokeandwait(new runnable()                      {                            @override                         public void run()                         {                             frame.getprogressmonitor().setprogress(percent * 10);                             frame.getprogressmonitor().setnote("completed " + percent*10 + "%.");                         }                     });                     try                     {                         thread.sleep(1000);                     }                     catch(exception ee)                     {                         //                     }                                            }             }             catch(exception es)             {                 //             }         }                }); }  /**  * create frame.  */ public progressmonitortest()  {     isframeready = false;      setdefaultcloseoperation(jframe.exit_on_close);     setbounds(100, 100, 450, 300);     settitle("progress monitor");      contentpane = new jpanel();     contentpane.setborder(new emptyborder(5, 5, 5, 5));     contentpane.setlayout(new borderlayout(0, 0));      progressmonitor = new progressmonitor(frame, "update in progress...", "", 0, 10);     button = new jbutton("click here");     contentpane.add(button);      setcontentpane(contentpane); } 

}

a few questions regarding this-

  1. if remove isframeready check, program says nullpointerexception @ line assign button's action listener.

  2. if keep above check, clicking on button nothing.

  3. keeping above check , debugging this, let wait time before gets line action listener. in case, works quits saying can't call invokeandwait event handling thread.

what missing in ? can explain how work.

if remove isframeready check, program says nullpointerexception @ line assign button's action listener.

your use of isframeready ensures have created frame successfully. inside main, posted request event dispatch thread(edt) using call eventqueue.invokelater(new runnable(){}): removing check isframeready, going call frame.getbutton() in main thread frame have not been yet created frame = new progressmonitortest(); in edt , nullpointerexception occurs.

if keep above check, clicking on button nothing.

you should understand now, above check nothing button click. button not doing because gui got freezed violating swing's single threading rule. put incrementing for loop of actionperformed method inside thread following code fragement shows , execute there. see works fine.

 new thread(){     public void run()     {        for(int i=0; i<10; i++)         {            //whatever doing.         }     }    }.start();  

keeping above check , debugging this, let wait time before gets line action listener. in case, works quits saying can't call invokeandwait event handling thread.

swingutitlies.invokeandwait() blocks current thread , waits until edt done executing task given it. actionperformed() function running inside edt, calling swingutitlies.invokeandwait() current thread:edt block current thread:edt should not allowed. don't use invokeandwait case. should call swingutilities.invokelater() instead.

however don't think until understand swing threading model. read javadoc , internet resource. have book filthy rich clients , try example book offered: have greater knowledge in graphical effects other resource can provide.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -