如何逐步更新JProgressBar?

时间:2014-02-28 00:10:23

标签: java swing jprogressbar

我希望获得波形声音的最大长度,播放曲目并使用JProgressBar显示其播放进度。这是我到目前为止所做的,但进度条没有更新。

Timer t = new Timer(1000,this);

private void playButtonMousePressed(java.awt.event.MouseEvent evt)   {                                        
    // TODO add your handling code here:

   try{
       t.start();
    String track = "tele";
    music = new MusicFunctions(track);
   // music.play(track);

    int progressInit = 0;
    final int len = music.trackLength();
    int tLength = music.trackLength();
    jProgressBar1.setMinimum(0);
    jProgressBar1.setMaximum(len);


    while(i<=len){
    jProgressBar1.setValue(i);
    jProgressBar1.repaint();
    i++;
    }

   }catch(Exception e){
       System.out.println(e);

}                                       
}

1 个答案:

答案 0 :(得分:-1)

请参阅SwingWorker文档;该示例甚至用于更新进度条:http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html