JoseRivas1998
JoseRivas1998

Reputation: 553

Font color in TextField from ColorPicker

I am building an application that involves a TextArea and a TextField in JavaFX. I would like to include the ability to change the font color of those with a ColorPicker. I was able to customize the background color pretty easily by doing the following

backgroundColorPicker.setOnAction(event -> {
    Color color = backgroundColorPicker.getValue();
    Background background = new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY));
    Region region = ( Region ) console.lookup( ".content" );
    region.setBackground(background);
    input.setBackground(background);
});

How would I change the font color? So far I only have

foregroundColorPicker.setOnAction(event -> {
    Color color = foregroundColorPicker.getValue();         
});

I have been unable to find a way to change the font color on the fields.

Upvotes: 1

Views: 405

Answers (1)

JoseRivas1998
JoseRivas1998

Reputation: 553

I was able to solve this by using the color value and converting it to CSS, then applying that CSS to the fields.

foregroundColorPicker.setOnAction(event -> {
    Color color = foregroundColorPicker.getValue();
    double red = color.getRed() * 255;
    double green = color.getGreen() * 255;
    double blue = color.getBlue() * 255;
    double alpha = color.getOpacity() * 255;
    String colorString = String.format("-fx-text-fill: rgba(%f,%f,%f,%f) ;", red, green, blue, alpha);
    console.setStyle(colorString) ;
    input.setStyle(colorString);
});

Upvotes: 1

Related Questions