Isabelle Congdon
Isabelle Congdon

Reputation: 11

Temporarily change color of a button when clicked

I need to temporarly change the background color of a clicked button for 0.5 seconds and then want it to revert back to the original color.

I have tried using pausetransition but im really new to java and am not sure how to use it correctly. Right now the best I can do is get he button to stay at the new color when clicked.

       Color[] colors = new Color[]{Color.DARKORCHID,Color.SALMON,Color.SPRINGGREEN,Color.GOLD};
        Color randomColor = colors[(new Random().nextInt(4))]; 
        button.setBackground(new Background(new BackgroundFill(randomColor,null,null)));
        grid.add(button, column, row);
         button.setOnAction(new EventHandler<ActionEvent>(){
          public void handle(ActionEvent e){
             Button b = (Button)e.getSource();
             Button save = b;
             b.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY,null,null)));
          }
        });
}

This right now is just changing the color to grey. If I could figure out how to get the color to temporarily change OR even change back when clicked again. Another part of my issue is that all the buttons have different colors. Any tips or help would be appreciated.

Upvotes: 0

Views: 1914

Answers (1)

Slaw
Slaw

Reputation: 45726

For each Button you create you need to create a PauseTransition, with a duration of half a second, that will set the background back to the original. When you click on the Button you would change the background to the grey background and restart the PauseTransition. This will make it so the background is reverted half a second after the last click. Here's a small example:

import java.util.Random;
import javafx.animation.Animation.Status;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setAlignment(Pos.CENTER);
        root.setHgap(10);
        root.setVgap(10);
        root.setPadding(new Insets(10));

        fillGrid(root);

        primaryStage.setScene(new Scene(root, 500, 300));
        primaryStage.show();
    }

    private static void fillGrid(GridPane grid) {
        Background[] backgrounds = {
            new Background(new BackgroundFill(Color.DARKORCHID, null, null)),
            new Background(new BackgroundFill(Color.SALMON, null, null)),
            new Background(new BackgroundFill(Color.SPRINGGREEN, null, null)),
            new Background(new BackgroundFill(Color.GOLD, null, null))
        };
        Background greyBg = new Background(new BackgroundFill(Color.GREY, null, null));

        Random rand = new Random();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Background original = backgrounds[rand.nextInt(backgrounds.length)];

                Button button = new Button(String.format("(%d,%d)", i, j));
                button.setBackground(original);

                PauseTransition transition = new PauseTransition(Duration.seconds(0.5));
                transition.setOnFinished(event -> button.setBackground(original));

                button.setOnMouseClicked(event -> {
                    event.consume();
                    button.setBackground(greyBg);
                    transition.playFromStart();
                });

                grid.add(button, j, i);
            }
        }
    }

}

Upvotes: 1

Related Questions