Christian H. Kuhn
Christian H. Kuhn

Reputation: 355

Bi-coloring JavaFX TableCell Background?

I have a javafx.scene.control.TableView containing some javafx.scene.control.TableColumns containing some javafx.scene.control.TextFields. Some of the TextFields should get a bi-coloured background: diagonally divided, the upper left half one colour, the lower right half another colour. Like the cells for Ge, Sb, Po in https://de.wikipedia.org/wiki/Periodensystem#/media/Datei:Periodensystem_deutsch_-_neue_Farben.svg. Is this possible without a background image?

Edit: added some code. For simplicity’s sake, i omitted the surrounding table and use now a javafx.scene.text.Text instead of a javafx.scene.control.TextField. The basic setup that follows gives a uniform background to the StackPane which is a start.

public class DividedBackgroundDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Test: Diagonally divided colour background");

    Text txt = new Text("Text");
    txt.setFont(Font.font("Arial", FontWeight.BOLD, FontPosture.REGULAR, 50));

    StackPane root = new StackPane();
    root.getChildren().add(txt);
    root.setStyle("-fx-background-color: blue;");

    primaryStage.setScene(new Scene(root, 200, 200));
    primaryStage.show();
    }

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

}

Later on, when i will use a 13x13 grid of Text, i will have to imbed each Text into it’s own StackPane or another (perhaps more appropriate) subclass of Region to be able to provide an individually coloured background for each Text. And some of those Texts (especially the one in this example) should have a bi-colour background, both colours divided by the line from left bottom to right upper corner.

The simple solution would be to provide a background image. It’s not too difficult to produce one. But then, i have to produce a new background image for every pair of colours. I would prefer a more flexible solution.

All property setters and CSS properties i have found provide a one-colour-background. But sometimes i require bi-colour.

The proposed LineGradient would be acceptable if everything else fails. I would prefer a clear border between both colours, not a gradient.

Upvotes: 1

Views: 72

Answers (1)

Christian H. Kuhn
Christian H. Kuhn

Reputation: 355

Ok, i found a solution, at least for the simplified problem. Thanks to @kleopatra. As a path, i used a javafx.scene.shape.Polygon and filled it with the appropriate colour. Using aStackPane`, it is important to watch the z coordinate. Background naturally bottom, filled polyline as first content in the pane, and text on top.

public class DividedBackgroundDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Test: Diagonally divided colour background");

        Text txt = new Text("Text");
        txt.setFont(Font.font("Arial", FontWeight.BOLD, FontPosture.REGULAR, 50));

        Polygon triangle = new Polygon();
        triangle.getPoints().addAll(new Double[] { 0.0, 0.0, 200.0, 0.0, 0.0, 200.0 });
        triangle.setFill(Color.YELLOW);

        StackPane root = new StackPane();
        root.getChildren().add(triangle);
        root.getChildren().add(txt);
        root.setStyle("-fx-background-color: blue;");

        primaryStage.setScene(new Scene(root, 200, 200));
        primaryStage.show();
    }

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

Upvotes: 2

Related Questions