title: How to change color of text in JavaFX Label
body: <p>I am having trouble changing colors of text that are within the JavaFX label class.</p>
  
  <p>This is the code I have so far.</p>
  
  <pre><code>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 &lt; 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) -&gt; 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 &lt; 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);
  }}
  </code></pre>
  
  <p>When I build it and play with the sliders, this is what it looks like.
  <a href="https://i.stack.imgur.com/TBAkxm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TBAkxm.png" alt="Color Changer"></a></p>
  
  <p>How can I edit the colors of text "Red", "Green", and "Blue", so the text colors matches the words like this?</p>
  
  <p><a href="https://i.stack.imgur.com/oAkvh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oAkvh.png" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>Please help, thank you.</p>