1

When a user does some action (such as click a button), I need to display the status in the JLabel. This status needs to disappear after 2 seconds. I use the following for that.

Timer timer = new Timer(2000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                label.setVisible(false);
        }
    });

However, it is possible that the user clicks the button many times and this timer is triggered multiple times. This has an undesirable effect. When a button is clicked 3 times for instances.

0th second: 1st click : label disppear at 2nd second

1st second: 2nd click : label disppear at 3rd second

2nd second: 3rd click : label disppear at 4th second

Here, the label needs to disappear after the 4th second but will disappear after 2nd second. Therefore, I want this label to be hidden with a delay of 2s after only the last event

To handle this, I use an atomic counter.

AtomicInteger counter = new AtomicInteger(0);

Each task is given a unique 'taskCounter' using counter.incrementAndGet()

.

  Timer timer = new Timer(5000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (counter.get() == taskCounter) {
                infoLabel.setVisible(false);
            }
        }
    });
    timer.start();

The above is only executed if it is the last event that has been triggered. Ensuring that, my label stays visible at least 2 seconds after the last event.

Is there a better way of dealing with this problem?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user592748
  • 1,194
  • 3
  • 21
  • 45
  • Please note: your question actually translates to "can you help me improve this code" ... and such questions better go to codereview.stackexchange.com. Here we help with "concrete" problems ... – GhostCat May 20 '16 at 09:17
  • What's about `Timer.setRepeats(false)`? – Sergiy Medvynskyy May 20 '16 at 09:20
  • @Jägermeister actually, my question is more like are there existing methods to deal with this problem. Am I reinventing the wheel? – user592748 May 20 '16 at 09:30
  • 1
    You could keep track of how long the `Timer` has been running, on each tick, make a determination about what you should do – MadProgrammer May 20 '16 at 11:19

1 Answers1

3

Given two instances of java.swing.Timer, let t1 fire at some some nominal rate that is less than two seconds. Let the ActionListener for t1 update the label and invoke restart() on t2, which should have an initial delay of two seconds. Let the ActionListener for t2 stop() itself and invoke label.setVisible(false).

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Invoke `t1.start()` in the button's `ActionListener`. If this does not work in the context of your actual program, please edit your question to include a [mcve] that focus on your approach. – trashgod May 20 '16 at 09:49
  • Ok, cool! Now I work with only one timer. I always restart it on the button click and then set the label to visible. So the timer always has to hide the label after 2 seconds. The overlap is not possible. – user592748 May 20 '16 at 11:54