title: JavaFX-CSS: How to "move" style of parent to a child?
body: <p>The trigger for this was a quick experiment to solve <a href="https://stackoverflow.com/q/23792004/203657">TreeItem selection width</a>: the requirement is to highlight only the text, not the whole tree cell. Nothing easier than that (said Frederik :) </p>
  
  <ul>
  <li>implement a custom TreeCell with a Label as graphic (and configure the label with the item as needed)</li>
  <li>remove the selection style (mostly the highlight background from the cell</li>
  <li>add the highlight style to the label</li>
  </ul>
  
  <p>Something like (a runnable example using this is at the end):</p>
  
  <pre><code>public static class MyTreeCell extends TreeCell&lt;String&gt; {
  
      private Label label;
  
      public MyTreeCell() {
          getStyleClass().add("tree-text-only");
      }
      @Override
      public void updateItem(String item, boolean empty) {
          super.updateItem(item, empty);
          if (empty) {
              setText(null);
              setGraphic(null);
          } else {
              if (label == null) {
                  label = new Label();
              }
              label.setText(item);
              setGraphic(label);
          }
      }
  }
  </code></pre>
  
  <p>The css:</p>
  
  <pre><code>/* remove the highlight from cell as a whole, 
     c&amp;p'd the unselected style from modena */
  .tree-text-only:filled:selected {
      -fx-background: -fx-control-inner-background; 
      -fx-background-color: -fx-background;
  }
  /* c&amp;p'd selected style */
  /* using &gt; doesn't make a different to the behaviour */ 
  /* .tree-text-only:filled:selected &gt; .label { */
  
  .tree-text-only:filled:selected .label {
      /* following is the selection color from themes, doesn't show */
      -fx-background: -fx-selection-bar; 
      /* hard-coded color does show */
      /*   -fx-background-color: -fx-accent ; */
      /* auto-adjust text fill */
      /* no effect for hard-coded color, showing nothing for selection bar */
      -fx-text-fill: -fx-text-background-color;
  }
  </code></pre>
  
  <p>Expected behaviour</p>
  
  <ul>
  <li>label has highlighted background when using selection-bar</li>
  <li>text auto-adjusts its fill</li>
  </ul>
  
  <p>Actual behaviour: </p>
  
  <ul>
  <li>using the "semantic" (?) highlight color selection-bar, the background color of the label is not changed to the highlight color but the text fill is changed to the "white", so the text doesn't show </li>
  <li>using a hard-coded color (any, even the accent as above) changes the label's background but the text fill isn't updated</li>
  </ul>
  
  <p>The obvious question: How to make it work as expected?</p>
  
  <p>For convenience, a runnable example:</p>
  
  <pre><code>public class TreeCellExample extends Application {
  
      public static class MyTreeCell extends TreeCell&lt;String&gt; {
  
          private Label label;
  
          public MyTreeCell() {
              getStyleClass().add("tree-text-only");
          }
          @Override
          public void updateItem(String item, boolean empty) {
              super.updateItem(item, empty);
              if (empty) {
                  setText(null);
                  setGraphic(null);
              } else {
                  if (label == null) {
                      label = new Label();
                  }
                  label.setText(item);
                  setGraphic(label);
              }
          }
      }
  
      private Parent getContent() {
          TreeItem root = createSubTree("root");
          root.setExpanded(true);
          TreeView tree = new TreeView(root);
          tree.getStylesheets().add(
                  getClass().getResource("treetextonly.css").toExternalForm());
          tree.setCellFactory(p -&gt; new MyTreeCell());
          BorderPane pane = new BorderPane(tree);
          return pane;
      }
  
      ObservableList rawItems = FXCollections.observableArrayList(
              "9-item", "8-item", "7-item", "6-item", 
              "5-item", "4-item", "3-item", "2-item", "1-item");
  
      protected TreeItem createSubTree(Object value) {
          TreeItem child = new TreeItem(value);
          child.getChildren().setAll((List&lt;TreeItem&gt;) rawItems.stream()
                  .map(TreeItem::new)
                  .collect(Collectors.toList()));
          return child;
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          Scene scene = new Scene(getContent());
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>