4

これのトリガーは、TreeItem の選択幅を解決するための簡単な実験でした: 要件は、ツリー セル全体ではなく、テキストのみを強調表示することです。それより簡単なことはありません(フレデリックは言いました:)

  • グラフィックとして Label を使用してカスタム TreeCell を実装します (必要に応じてアイテムでラベルを構成します)
  • 選択スタイルを削除します (ほとんどの場合、セルからハイライトされた背景)
  • ハイライト スタイルをラベルに追加する

のようなもの (これを使用した実行可能な例は最後にあります):

public static class MyTreeCell extends TreeCell<String> {

    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);
        }
    }
}

CSS:

/* remove the highlight from cell as a whole, 
   c&p'd the unselected style from modena */
.tree-text-only:filled:selected {
    -fx-background: -fx-control-inner-background; 
    -fx-background-color: -fx-background;
}
/* c&p'd selected style */
/* using > doesn't make a different to the behaviour */ 
/* .tree-text-only:filled:selected > .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;
}

期待される動作

  • 選択バーを使用すると、ラベルの背景が強調表示されます
  • テキストは塗りつぶしを自動調整します

実際の動作:

  • 「セマンティック」(?) ハイライト色の選択バーを使用すると、ラベルの背景色はハイライト色に変更されませんが、テキストの塗りつぶしは「白」に変更されるため、テキストは表示されません
  • ハードコーディングされた色 (任意、上記のアクセントも含む) を使用すると、ラベルの背景が変更されますが、テキストの塗りつぶしは更新されません

当然の質問: 期待どおりに機能させるにはどうすればよいでしょうか?

便宜上、実行可能な例:

public class TreeCellExample extends Application {

    public static class MyTreeCell extends TreeCell<String> {

        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 -> 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<TreeItem>) 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);
    }
}
4

2 に答える 2

3

が機能しない理由は、 が適用される.labelルールがないため-fx-backgroundです。そのため、割り当てられた値はどのLabelプロパティにも影響しません。

さらに、プロパティLabelを使用しません。-fx-background-color

したがって、簡単な解決策はそれを追加することです:

.tree-text-only > .label {
    -fx-background-color: -fx-background; 
}

これで、この「セマンティック」カラーのみを使用してすべてのスタイルを適用できます。

コントロールにフォーカスがない場合も追加したことに注意してください。

/* 
 * remove the highlight from cell as a whole 
 */

/* Selected rows */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:filled:selected {
    -fx-background: -fx-control-inner-background; 
}
/* Selected when control is not focused */
.tree-text-only:filled:selected {
    -fx-background: -fx-control-inner-background; 
}
/* focused cell (keyboard navigation) */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:focused {
    -fx-cell-focus-inner-border: -fx-control-inner-background; 
}

/* 
 * highlight only the label
 */

// Add background color rule
.tree-text-only > .label {
    -fx-background-color: -fx-background; 
}
/* Selected rows */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:filled:selected > .label {
    -fx-background: -fx-selection-bar; 
}
/* Selected when control is not focused */
.tree-text-only:filled:selected > .label {
    -fx-background: -fx-selection-bar-non-focused;
}
/* focused cell (keyboard navigation) */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:focused > .label {
    -fx-background: -fx-selection-bar; 
}
于 2015-01-23T20:20:41.827 に答える
1

繰り返しますが、私はあなたの要件を100%理解しているとは確信していませんが、次の簡単なテストはうまくいきます(Labelのグラフィックプロパティに割り当てられていますButton):

.button {
    -fx-text-fill: red;
    -fx-background-color: yellow;
}

.button > .label {
    -fx-text-fill: blue;
    -fx-background-color: yellow;
}

.button:pressed > .label {
    -fx-text-fill: white;
    -fx-background-color: black;
}
于 2015-01-23T16:18:52.387 に答える