Progress bar not changing
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
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.
精彩评论