簡體   English   中英

JavaFX-CSS:如何將父母的風格“移動”給孩子?

[英]JavaFX-CSS: How to “move” style of parent to a child?

觸發器是解決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);
    }
}

它不起作用的原因是因為.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; 
}

同樣,我不是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM