I am creating a mini game which contains a set of labels that start out as black, but turn yellow once it's clicked. What I want to happen is for the previously clicked label to turn back to black when the currently clicked label is turned yellow.
The problem I'm having doing this is working out how to turn the previously clicked label back to black. I've tried creating a new GridPane
every time a label is clicked and using different classes to do different things and none of it has worked. I've also tried creating a 2D array of labels while creating my GridPane
but I can't manage to use it to reset previous labels.
Here is my code, where am I going wrong?
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.InputEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane borderpane = new BorderPane();
GridPane grid = setGrid();
borderpane.setTop(grid);
Scene sc = new Scene(borderpane, 400, 400);
sc.getStylesheets().add("application/application.css");
primaryStage.setScene(sc);
primaryStage.show();
}
public GridPane setGrid() {
GridPane grid = new GridPane();
for (int row = 0; row < 5; row++)
for (int col = 0; col < 5; col++) {
Label l = new Label();
l.setPrefHeight(100);
l.setPrefWidth(100);
l.getStyleClass().add("box");
l.setOnMouseClicked(new EventHandler<InputEvent>() {
@Override
public void handle(InputEvent arg0) {
l.getStyleClass().add("clicked");
}
});
grid.add(l, col, row);
}
return grid;
}
public static void main(String[] args) {
Application.launch(args);
}
}