How to change TreeItem's underline color in JavaFX?

Viewed 1300

I have a JavaFX TreeView which has TreeItems with two basic colors , red and white . When they are selected they both have -fx-underline:true but i have a problem with the line being Red or White i want the underline to be always white , is that possible with css in JavaFX ?

enter image description here

I have tried the below but it doesn't work .

.tree-cell-white:selected {
    -fx-font-weight: bold ;
    -fx-text-fill:white; 
    -fx-underline: true;
    -fx-underline-fill: orange;
    -fx-underline-color: blue;
}

.tree-cell-red:selected {
 -fx-font-weight: bold ;
 -fx-text-fill:firebrick; 
 -fx-underline: true;
}
1 Answers

Unfortunately I could not find how to do it by JavaFX CSS, but I found another solution. That is I created MyTrack class that derives from HBox and added an ImageView as an music/mp3 icon and Label with text. Here is a little trick I underlined label by setting its bottom border color and set MyTrack as a graphic for TreeItem.

package underlined.treeview;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.List;

public class TreeViewApp extends Application {

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

    private Parent createContent() {
        TreeItem<String> treeRoot = new TreeItem<>("Root node");
        List<TreeItem<String>> tracks = Arrays.asList(
                new TreeItem<>(""),
                new TreeItem<>(""),
                new TreeItem<>(""));

        for (TreeItem<String> track : tracks)
            track.setGraphic(new MyTrack("Track"));

        treeRoot.getChildren().addAll(tracks);

        TreeView treeView = new TreeView();
        treeView.setShowRoot(true);
        treeView.setRoot(treeRoot);
        treeRoot.setExpanded(true);
        return treeView;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(createContent());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

and MyTrack class:

package underlined.treeview;

import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

public class MyTrack extends HBox {

    public MyTrack(String text) {

        Label label = new Label(text);
        label.setStyle("-fx-border-color: aqua;" +
                "-fx-border-width: 0 0 1 0;");

        Image image = new Image(getClass().getResource("/img/icon/mp3.png")
                .toExternalForm());
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(16);
        imageView.setFitHeight(16);

        getChildren().addAll(imageView, label);
    }
}

And Voila!!!

In my example bottom border color is aqua here. Note that TreeItem should be instantiated with empty String. It was just an example you need to optimize it yourself.

Related