I want you to be able to see how many times the button was pressed and I would like to make these ads with colors. Short explanation light green the button was pressed once and dark red the button was pressed very often.
I have solved this solution of the problem with a simple if.
If so (clicksCounter == 1) then green
But this solution is not very elegant and I would need many if queries from light green to dark red. Now my question is someone knows a way how I can change the color of light green to dark red fluently?
By fluent I mean that it takes from light green to dark green and the "values in between" to make it look more beautiful.
And not, as in my example, only green, yellow and red, but that the color change should be smooth.
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javax.security.auth.callback.Callback;
import java.net.URL;
import java.time.LocalDate;
import java.util.Date;
import java.util.ResourceBundle;
public class Controller implements Initializable {
private int clicksCounter = 0;
private String date = String.valueOf(LocalDate.now().getDayOfMonth());
@FXML
private Button button2;
@FXML
private DatePicker datePicker;
@FXML
private Label lbColor;
@Override
public void initialize(URL location, ResourceBundle resources) {
button2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println(clicksCounter);
clicksCounter++;
if(clicksCounter == 1 ) {
lbColor.setStyle("-fx-background-color: #04b404;");
}
else if(clicksCounter == 2) {
lbColor.setStyle("-fx-background-color: #ffff13;");
}
else {
lbColor.setStyle("-fx-background-color: #df0101;");
}
}
});
javafx.util.Callback<DatePicker, DateCell> set = new javafx.util.Callback<DatePicker, DateCell>() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
//if today, change text and style
if (item.equals(LocalDate.now())) {
setText(date +"/" + clicksCounter);
if(clicksCounter == 1 ) {
setStyle("-fx-background-color: #04b404;");
}
else if(clicksCounter == 2) {
setStyle("-fx-background-color: #ffff13;");
}
else {
setStyle("-fx-background-color: #df0101;");
}
}
}
};
}
};
datePicker.setDayCellFactory(set);
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="button2" layoutX="280.0" layoutY="48.0" mnemonicParsing="false" text="Click" />
<DatePicker fx:id="datePicker" layoutX="45.0" layoutY="56.0" />
<Label fx:id="lbColor" layoutX="191.0" layoutY="155.0" text="Color" />
</children>
</AnchorPane>
Thank you in advance.