Reputation: 29
I'am new to JavaFX. I want to do something like in the following image inside my TextArea. I think it can be done using the Label and set the background color of it. But how?
Upvotes: 1
Views: 1066
Reputation: 49185
It can be done by putting the Label
into some layout container, say HBox
:
private final Random random = new Random();
private final Color[] colors =
{
Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW
};
@Override
public void start( final Stage primaryStage )
{
HBox hbox = new HBox();
String str = "my-string-val";
for ( String s : str.split( "" ) )
{
Label l = new Label( s );
l.setBorder( new Border( new BorderStroke( Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT ) ) );
l.setBackground( new Background( new BackgroundFill( colors[random.nextInt( colors.length )], CornerRadii.EMPTY, Insets.EMPTY ) ) );
l.setPrefWidth( 20 );
l.setAlignment( Pos.CENTER );
l.setFont( font( "Arial", FontWeight.BOLD, 16 ) );
hbox.getChildren().add( l );
}
final Scene scene = new Scene( hbox, 800, 600 );
primaryStage.setScene( scene );
primaryStage.show();
}
Upvotes: 2