turbolocust
turbolocust

Reputation: 314

How to change the color of the placeholder text in JavaFX TableView?

I believe it is quite simple, though I couldn't figure it out yet:
How do I change the color of the placeholder text in JavaFX TableView?

This is the placeholder text, which is shown if the table is empty:

JavaFX TableView (dark design)

Upvotes: 1

Views: 2329

Answers (1)

Oliver Jan Krylow
Oliver Jan Krylow

Reputation: 1725

There are (at least) two ways to solve this.

Via Css

Consult the JavaFx Css Reference and there you will see, that a TableView has an internal placeholder, that you can address using Css.

If documentation is insufficient or you need to more information about the structure of the Scenegraph, use ScenicView to explore.

Via placeholder node

Consulting the JavaFx 8 Api documentation will reveal, that there is a placeholder property, that allows you to set a custom node as placeholder.

Demo

This demo showcases both approaches:

TableViewPlaceholderFill.java:

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class TableViewPlaceholderFill extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        TableView<String> tableViaCss = new TableView<>();
        tableViaCss.getStyleClass().add("my-little-pony");

        TableView<String> tableWithCustomPlaceholder = new TableView<>();
        final Label placeholderLabel = new Label
                ("Hello, Kitty!");
        placeholderLabel.setFont(Font.font("monospace", FontWeight.BLACK, 16));
        placeholderLabel.setTextFill(Color.HOTPINK);
        tableWithCustomPlaceholder.setPlaceholder(new StackPane(placeholderLabel));

        Scene scene = new Scene(new HBox(4,tableViaCss,
                tableWithCustomPlaceholder));
        scene.getStylesheets().add(TableViewPlaceholderFill.class
                .getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

application.css:

.my-little-pony {
     -fx-background-color: palevioletred;
 }

 .my-little-pony .placeholder .label {
     -fx-text-fill: linen;
     -fx-font-family: 'serif';
     -fx-font-size: 1.666em;
     -fx-font-weight: bold;
 }

Upvotes: 1

Related Questions