0

During my free time, I'm practicing java by making a game. I have a button that calls 2 methods one after the other and both of them alter the same UI object.

I can't find a way/can't understand how to make java wait between execution of the methods so that I could see the value of the field after the first method completes and before it is changed again by the second method.

The way I understand, the best way is to use a swing timer, but I simply didn't understand it's syntax. I tried using sleep, but even though it waited the time specified, it only showed the original and final values, omitting the one in between.

What I have is:

label.setIcon(Alive);

private void cmdAttackActionPerformed(java.awt.event.ActionEvent evt) {
    //inside method1:
    label.setIcon(Dead);

    //wait a few seconds to see the label while setIcon(Dead) is the value

    //inside method2:
    if (reinforcements!=0){
        label.setIcon(Alive);
    }
}

So I tried tinkering some and managed to get results, but I still can't get it to do what I want.

public void run() {
    String output="";
    for (int i = 0; i < combatInfo[info].length(); i++) {
        output=output+combatInfo[info].substring(i, i+1);
        if(i%2==0){
            cmdInfo.setText(output);
            System.out.println(output);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {}
    }
};

The println() works one step at a time, as it should, however, the setText() still only displays the final value, skipping anything in between. How do I fix that?

Tomas
  • 91
  • 1
  • 3
  • It sounds like you want to use a debugger to inspect the value after the first method call? Or am I wrong? – Danny Apr 14 '15 at 13:29
  • possible duplicate of [Wait function in Java](http://stackoverflow.com/questions/2517022/wait-function-in-java) – Davis Broda Apr 14 '15 at 13:31
  • Try invoking the method at the end of the first one – CaffeineToCode Apr 14 '15 at 13:33
  • I don't want a debugger, I know the value that appears, I just want the program to wait for me to actually see it. The idea is of reinforcements - as the character, you fight against a maximum of 3 enemies at a time, but there can be more waiting and they replace enemies that have been defeated. I want to show a defeated enemy on a label, before replacing it with a new one. If there are no more replacements (the second method does nothing) the "defeated" picture is show without problems. – Tomas Apr 14 '15 at 14:43

2 Answers2

1

Tomas, there have been other users here on stackoverflow with the same (very similar question). I suggest you search before you post.

You can use a Timer as you say, see this answer as an example: java: run a function after a specific number of seconds

Basically you would invoke method1 and use the timer to invoke method2 in the future (scheduled).

Community
  • 1
  • 1
  • I have looked for solutions before asking. The problem is that I didn't understand the solution at all or I couldn't test it because a timer is non-static and I'm only vaguely familiar with the concept. I just want an example of how to add the timer to a method called by a button. – Tomas Apr 14 '15 at 14:53
  • The answer should be a comment rather than an answer. – minion Apr 14 '15 at 15:10
0

The best way would be to use a debugger and set a breakpoint - any modern IDE has support for that :)

Vadim Landa
  • 2,784
  • 5
  • 23
  • 33