开发者

jSlider code causes the application to hang

This jSlider code causes the application to hang.

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:               
        try {
              for(int i=0;i<100;i++)
                {
            jSlider1.setValue(i);
            Thread.sleep(3000);
              }
        } catch (InterruptedException ex) {
            Logger.getLogger(AsdView.class.getName()).log(Level.SEVERE, null, ex);
   开发者_开发百科     }              
}

thank you guys i am updating the answer

 Timer time = new Timer(100, new ActionListener() {
                  int percent = 0;

                @Override
                public void actionPerformed(ActionEvent e) {
                        percent++;
                        if (percent>100)
                                percent = 0;

                     jSlider1.setValue((int)(100*(percent/600.0)));
                }
        });
        time.start();


I am guessing you are trying to do some kind of smooth scroll.

That code freezes because the event thread that handles the window painting, sizing, etc..., is being frozen by you executing Thread.sleep(3000), 100 times.

I would recommend that you use a Swing timer that changes the scroll bar little by little.


i don't know why you put the sleep for 3 secs and that too in for loop of 100 3*100 = 5 mins so it will hang upto 5 mins remove Thread.sleep(3000);

it will work fine and won't hang


When you say your application 'hangs', that generally means that you have some sort of deadlock that prevents your threads from making progress. Is that what you're observing in your program? The way it's currently written, the current thread of execution will take at least 300 seconds to complete. Is this work being done on the main thread of execution? If so, you may want to consider creating a new Thread to do this work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜