0

So basically i have a JOptionPane which will print out an int, sort of like a progress update. I also have a while loop that executes some code and every time it runs it adds 1 to a int. Now i would like to have the joptionpane update once in a while with the current number that the int has. ive been trying to do this with a timer but my 16 y/o stubborn mind cant figure it out! any help is greatly appreciated.

Heres some example code

int printNumber = 0, loops = 100;
while(loops != 100){
    ...
    printNumber++;
}
JOptionPane.showMessageDialog(frame, "\nCurrently Loading... "+ printNummber, "Loading", JOptionPane.INFORMATION_MESSAGE);

now i'd like the OptionPane to update the value printNumber every few seconds, i cant figure it out xD thanks in advance!! :)

camickr
  • 321,443
  • 19
  • 166
  • 288
Fallspell
  • 45
  • 1
  • 1
  • 6
  • 1
    I wouldn't use a `JOptionPane` for this... They are meant to show up and the go away not have updating graphics etc... Just make your own `JFrame` with a paint method updating the count. – 3kings Jan 29 '17 at 03:33
  • Ok thanks a lot ill try that i guess – Fallspell Jan 29 '17 at 03:38
  • Also one thing to note that typically `JOptionPane` blocks the code from running until a button is selected on it. Its like waiting for user input. that is why you can't use one in this situation. – 3kings Jan 29 '17 at 03:43
  • 1
    You'll probably want to do something like [this](http://stackoverflow.com/questions/15199091/progress-bar-java/15199220#15199220), see [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details. The other choice would be to use a Swing `Timer` which allows you to update the UI periodically – MadProgrammer Jan 29 '17 at 03:54
  • You would probably want to use a progress bar for something like this. Read the section from the Swing tutorial on [How to Use Progress Bars](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html) for more information and working examples. – camickr Jan 29 '17 at 04:13
  • 3
    @3kings, `with a paint method updating the count.` - why would you write a paint method? Just change the text of a JLabel. – camickr Jan 29 '17 at 04:14

0 Answers0