0

I'm aware of all the other similar questions but i cannot seem to fix the problem. I've tried "bar.repaint();" and "bar.update(bar.getGraphics());" but none of them seems to work.

If anyone has time to take a quick look at it i'd really appreciate it! It's really bugging me, i've been trying to solve it for hours now.

It's basically a shutdown timer. You input hours and seconds and it counts down until it shuts down the computer. It also has a progressbar, which is the problem. It doesn't seem to want to repaint itself after each second.

It's a lot of code, so i decided to just upload all of the files (2 files).

speedyshare.com/files/29072975/files.zip

Thanks in advance!

EDIT:

Snippet of the code that isn't working:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try{
        if(varHours.getText() != null && varMins.getText() != null){
            Thread countDownThread = new Thread(new Countdown(Integer.parseInt(varHours.getText()), Integer.parseInt(varMins.getText())));
            int totalSecs = (Integer.parseInt(varHours.getText())*60*60) + (Integer.parseInt(varMins.getText())*60);
            shutdownProgress = new javax.swing.JProgressBar(0, totalSecs);
            countDownThread.start();
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Please supply both fields!", "One or more fields were not supplied", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }catch(Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, "Error!\nCould not launch method countDown!", "Error!", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButton1ActionPerformed
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
qwerty
  • 35
  • 4
  • 4
    Please post a simplified portion of your code here so we can view it without going to the link you posted. – jjnguy Jun 20 '11 at 20:26
  • 1
    Have a look over this [CountDownProgressBar](http://stackoverflow.com/questions/5931933/how-to-make-timer-countdown-along-with-progress-bar/5932222#5932222) for tips. It is a complete example in less than 40 lines of code. – Andrew Thompson Jun 20 '11 at 20:41

1 Answers1

1

In jButton1ActionPerformed you set shutdownProgress. However, this is a new JProgressBar, not the one created in initComponents(). So, any changes you later make to shutdownProgress are made to a JProgressBar that is not displayed.

Secondly, making changes to a Swing component from a thread other than the EDT is a major no-no. Use SwingUtilitities .invokeLater.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71