Search code examples
javafxcolor-picker

Changing Text Area string colors in javaFX using the ColorPicker


I am trying to use the ColorPicker in JavafX to change the color of current text in a TextArea. Is there something like JavaSwing to change foreground color?


Solution

  • Set the value of the text area using setStyle on value change of the ColorPicker.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class TextAreaDemo extends Application {
        @Override
        public void start(Stage stage) {
            final TextArea textArea = new TextArea("Text Sample");
            textArea.setStyle("-fx-text-fill: black;");
            textArea.setPrefSize(200, 40);
    
            final ColorPicker colorPicker = new ColorPicker(Color.BLACK);
            colorPicker.valueProperty().addListener((observable, oldColor, newColor) ->
                textArea.setStyle(
                  "-fx-text-fill: " + toRgbString(newColor) + ";"
                )
            );
    
            stage.setScene(
                new Scene(new VBox(textArea, colorPicker), 300, 250)
            );
            stage.show();
        }
    
        private String toRgbString(Color c) {
            return "rgb("
                              + to255Int(c.getRed())
                        + "," + to255Int(c.getGreen())
                        + "," + to255Int(c.getBlue())
                 + ")";
        }
    
        private int to255Int(double d) {
            return (int) (d * 255);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }