3

I want to change the background color of the textarea in SceneBuilder.
I failed to change in the style menu :-fx-background-color.
So I found to change the background color by using the CSS file.

.text-area .content{
  -fx-background-color: red;
}

But I want to change the other way except for the css file. Please give me a hint .

jake
  • 669
  • 2
  • 8
  • 14

2 Answers2

6

You can change it in Java code:

@Override
public void start( Stage stage )
{
    TextArea area = new TextArea();
    Scene scene = new Scene( area, 800, 600 );
    stage.setScene( scene );
    stage.show();

    Region region = ( Region ) area.lookup( ".content" );
    region.setBackground( new Background( new BackgroundFill( Color.BROWN, CornerRadii.EMPTY, Insets.EMPTY ) ) );

    // Or you can set it by setStyle()
    region.setStyle( "-fx-background-color: yellow" );
}

To do that we first lookup the child Region sub structure of text area then apply styling on it. This action should be done after the stage has been shown.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
1

I just found the solution to change the color of the background of TextArea in JavaFX. Write this in your controller class:

textarea.setStyle("-fx-control-inner-background:#000000;");

I was deep searching on the stackoverflow and eventually found it. The link is given below: Textarea javaFx Color

Happy coding!