Reputation: 155
I am having trouble changing colors of text that are within the JavaFX label class.
This is the code I have so far.
package Problem2;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Problem2Code extends Application {
Slider[] slider = new Slider[4];
@Override
public void start(Stage primaryStage) throws Exception {
Text text = new Text("Show Colors");
// Bottom pane
Label[] labels = new Label[4];
String[] stringLabels = {"Red", "Green", "Blue", "Opacity"};
GridPane gridPane = new GridPane();
gridPane.setHgap(30);
gridPane.setVgap(5);
gridPane.setPadding(new Insets(25));
gridPane.setAlignment(Pos.CENTER);
for (int i = 0; i < slider.length; i++) {
slider[i] = new Slider();
slider[i].setMin(0);
if (!stringLabels[i].equals("Opacity")) {
slider[i].setMax(255);
slider[i].setValue(255);
} else {
slider[i].setMax(1);
slider[i].setValue(1);
}
labels[i] = new Label(stringLabels[i]);
slider[i].valueProperty()
.addListener((obser, old, newV) -> text.setFill(getColor()));
gridPane.add(labels[i], 0, i);
gridPane.add(slider[i], 1, i);
}
StackPane stackPane = new StackPane(text);
stackPane.setPrefSize(315, 65);
BorderPane borderPane = new BorderPane(stackPane);
borderPane.setBottom(gridPane);
primaryStage.setScene(new Scene(borderPane));
primaryStage.setTitle("Color Changer");
primaryStage.show();
}
private Color getColor() {
// r g b o
double[] rgb = new double[4];
for (int i = 0; i < rgb.length; i++) {
rgb[i] = slider[i].getValue();
}
return Color.rgb((int)rgb[0], (int)rgb[1], (int)rgb[2], rgb[3]);
}
public static void main(String[] args) {
Application.launch(args);
}}
When I build it and play with the sliders, this is what it looks like.
How can I edit the colors of text "Red", "Green", and "Blue", so the text colors matches the words like this?
I believe it has to do something with making an HBox? I tried it with that but didn't know how to do it correctly. I also tried making variables stringLabels1, stringLabels2, stringLabels3, and stringLabels4 for each of the strings, but had trouble with that in the gridPane portion. Having trouble coding either of those.
Please help, thank you.
Upvotes: 9
Views: 40429
Reputation: 18792
You could use Color#web method:
for (int i = 0; i < slider.length; i++) {
slider[i] = new Slider();
slider[i].setMin(0);
labels[i] = new Label(stringLabels[i]);
if (!stringLabels[i].equals("Opacity")) {
slider[i].setMax(255);
slider[i].setValue(255);
labels[i].setTextFill(Color.web(stringLabels[i])); //css alternative: labels[i].setStyle("-fx-text-fill: " + stringLabels[i]);
} else {
slider[i].setMax(1);
slider[i].setValue(1);
}
slider[i].valueProperty()
.addListener((obser, old, newV) -> text.setFill(getColor()));
gridPane.add(labels[i], 0, i);
gridPane.add(slider[i], 1, i);
}
ChangeListener<Number> listener = (obser, old, newV) -> text.setFill(getColor());
for (int i = 0; i < slider.length; i++) {
..
slider[i].valueProperty().addListener(listener);
..
}
Upvotes: 8
Reputation: 655
Use setTextFill
on label, below will set text color to Red:
labels[i].setTextFill(Color.color(1, 0, 0));
Upvotes: 12