From d1a8c2522dd10ca112ccda0cf1407814b772e6e6 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 27 Apr 2015 21:44:11 +0200 Subject: Stop the interval on command or after firing the action event --- .../inf/java2/aufgabe04/CountdownExitButton.java | 48 +++++++++++++++------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java index b859b8f..4ba5bf2 100644 --- a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java +++ b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java @@ -39,20 +39,21 @@ public class CountdownExitButton extends Button timeline .getKeyFrames() .add(new KeyFrame( - Duration.seconds(1), - (e -> { + Duration.seconds(1), + (e -> { CountdownExitButton.this - .setText(CountdownExitButton.this - .getText() - .replaceFirst( - "\\(" - + CountdownExitButton.this.countdown - + "\\)$", - "(" - + --CountdownExitButton.this.countdown - + ")")); + .setText(CountdownExitButton.this + .getText() + .replaceAll( + "\\(" + + CountdownExitButton.this.countdown + + "\\)$", + "(" + + --CountdownExitButton.this.countdown + + ")")); if (CountdownExitButton.this.countdown <= 0) { + CountdownExitButton.this.stopCountdown(); CountdownExitButton.this.fire(); } }))); @@ -64,10 +65,21 @@ public class CountdownExitButton extends Button */ public void startCountdown() { - super.setText(getText() + " (" + countdown + ")"); + setText(getText() + " (" + countdown + ")"); timeline.playFromStart(); } + /** + * Stops the countdown without the button fire event. + * + */ + public void stopCountdown() + { + timeline.stop(); + setText(getText().replaceAll( + "\\(" + CountdownExitButton.this.countdown + "\\)$", "")); + } + /** * Set the countdown. * @@ -86,12 +98,20 @@ public class CountdownExitButton extends Button /** * Get the countdown. * - * @return - * The countdown in seconds. + * @return The countdown in seconds. */ public int getCountdown() { return countdown; } + /* (non-Javadoc) + * @see javafx.scene.control.Button#fire() + */ + @Override + public void fire() + { + stopCountdown(); + super.fire(); + } } -- cgit v1.2.3-70-g09d2