-2

I have for loop here:

for(int i=0; i<=10; i++)
            {
            a = r.nextInt(y - x + 1) + x;
            label5.setText("Losowanie... " + a);
            try {
                Thread.sleep(100);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

            }

I want to use invokeAndWait to put "a" to label5 every time (loop), nut i don't know how to use it. Can somebody tell me about that?

Persantarus
  • 37
  • 1
  • 6
  • `invokeAndWait(() -> label5.setText("Losowanie... " + a));` But `invokeLater` should offer better precision in frame rate. – Marko Topolnik Oct 29 '16 at 16:10
  • The method invokeAndWait(() -> {}) is undefined for the type Losowanie.ListenForButton - thanks, but i don't have the first part of it... what should i get there? I mean x.invokeAndWait(() -> label5.setText("Losowanie... " + a)); - What should be my x? – Persantarus Oct 29 '16 at 16:12
  • With an `import static java.awt.EventQueue.invokeAndWait`, nothing. – Marko Topolnik Oct 29 '16 at 16:26

3 Answers3

2

You don't want to use invokeAndWait(). That won't help because you are still using Thread.sleep() with means the GUI can't repaint itself until the loop have finished executing.

Instead you need to use a Swing Timer.

After reading the tutorial for the Timer basics you can also check out: Update a Label with a Swing Timer for a simple example.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The code OP gives is exactly what you can run on a non-GUI thread, if you use `invokeAndWait`. Your answer seems to take for granted that OP is targetting the code to the GUI thread. – Marko Topolnik Oct 29 '16 at 16:17
  • Timer timer = new Timer(100, this); timer.start(); for(int i=0; i<=10; i++) { a = r.nextInt(y - x + 1) + x; label5.setText("Losowanie... " + a); } timer.stop(); If i do that, doesn't working. – Persantarus Oct 29 '16 at 16:39
  • @MarkoTopolnik, `Your answer seems to take for granted that OP is targetting the code to the GUI thread` - Yes because that is usually what happens when the user clicks on a button or something to start some kind of processing. – camickr Oct 29 '16 at 16:55
  • @Persantarus I see no reason why this code _would_ work. It's nonsense. – Marko Topolnik Oct 29 '16 at 16:56
  • @camickr I'm fine with your educated guess, but since OP hasn't stated it in the question, the answer is usable only to a reader who knows enough to realize that's a prerequisite for your suggestion to work. – Marko Topolnik Oct 29 '16 at 16:58
2

Assuming that this is done within a background thread, you first of all wouldn't even use invokeAndWait but rather would use invokeLater. You'd wrap the label5.setText(...) in a Runnable and pass it into invokeLater(...).

for(int i=0; i<=10; i++) {
    // made final to pass into inner class
    final int finalA = r.nextInt(y - x + 1) + x;
    SwingUtilities.invokeLater(() -> {
        label5.setText("Losowanie... " + finalA);
    });
    try {
        Thread.sleep(100);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}

Better solution still, if the code is in the event thread, then just use a Swing Timer

int delay = 100;
new Timer(delay, new ActionListener() {
    private int i = 0;
    private final maxI = 10;

    public void actionPerformed(ActionEvent e) {
        if (i < maxI) {
            String a = r.nextInt(y - x + 1) + x;
            label5.setText("Losowanie... " + a);
        } else {
            ((Timer) e.getSource()).stop();
        }
        i++;
    }
}).start();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-1
for(int i=0; i<=10; i++)
            {
                a = r.nextInt(y - x + 1) + x;
                label5.setText("Losowanie... " + a);
                Timer timer = new Timer(100, this);
                timer.start(); 

            }

Like that, doesn't work.

Like this, work, but all along and after loop so....

Timer timer = new Timer(100, new ActionListener()
                     {
                     @Override
                     public void actionPerformed(ActionEvent e)
                     {
                         for(int i=0; i<=10; i++)
                            {
                                a = r.nextInt(y - x + 1) + x;
                                label5.setText("Losowanie... " + a);  
                            }
                     }
                     });
             timer.start();
Persantarus
  • 37
  • 1
  • 6
  • You miss the point of the Timer. The Timer will generate an event every 100ms and then you update your label when the event fires. Did you look at my example??? There is no need for the loop. The Timer replaces the loop. – camickr Oct 29 '16 at 16:52
  • Ye, i got it, but still don't know what you mean. My timer starts after giving Dialog with random number. I want to start him when the randomizing starts, he gives numbers to label5 about 10 times or whatever i give and then stop and give Dialog. – Persantarus Oct 29 '16 at 16:59
  • Since Hovercraft Full Of Eels gave a full working example, i really don't see the point of this. – Marko Topolnik Oct 29 '16 at 16:59
  • His example do exactly the same as i wrote above + label5 last number is diffrent from that in Dialog. xD – Persantarus Oct 29 '16 at 17:01
  • His example is correct, unlike yours. If it doesn't work for you, it just means you weren't able to properly use it. – Marko Topolnik Oct 29 '16 at 17:12
  • `His example do exactly the same` - it is not the same. There is no loop in the correct answer. Only your code is using the Timer incorrectly by using a loop. – camickr Oct 29 '16 at 18:39