-1

I need to make a message dialog with JOptionPane that auto refreshes every second in java, for example a message dialog that display the numbers from the 1 to 100 in 100 seconds, I'm new at programming so I tried it making a cycle like this

import javax.swing.JOptionPane;
public class example{
    public static void main(String[] args){
        int n = 1;
        while(n<=100){
            JOptionPane.showMessageDialog(null, n);
            n++;
        }
    }
}

the problem: It makes alot of windows, and doesn't refresh the number, and I need only one message dialog.

2 Answers2

1

You can't do it that way as the line:

JOptionPane.showMessageDialog(null, n);

will cause a new dialog to be created each time it is executed. Instead you should extend JDialog and add a field for the increment and then execute the code that increments it using a SwingWorker.

Craig
  • 2,286
  • 3
  • 24
  • 37
0

In this link you can view another sort of post about this. If you check out the JOptionPane Documentiation you can see all it's methods with parameters.

Another thing that you could do, is create your own JDialog with specific methods to update some text.

Community
  • 1
  • 1
kevintjuh93
  • 1,010
  • 7
  • 22