使用CellFactory时,Javafx ListView选择栏文本颜色

时间:2012-06-16 10:49:17

标签: css listview javafx-2

有没有办法在列表视图中更改选择栏文本颜色? 最好使用CSS。在TableView中,您可以使用:

-fx-selection-bar-text: white;

但这对ListView不起作用。

更新:使用CellFactories渲染单元格时会出现上述情况。

lvRooms.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override public ListCell<String> call(ListView<String> list) {
            return new RoomCell();
        }
    });

在Cell Factory课程中,我很乐意报道选择该行的情况。

但是:在开头只调用一次,而不是每次移动选择栏时,因此isSelected()方法总是呈现为false。

更新2 :这是RoomCell实施:

class RoomCell extends ListCell<String> {
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (item != null) {
                Log.debug("RoomCell called, item: "+item);
                final Label lbl = new Label(item); // The room name will be displayed here
                lbl.setFont(Font.font("Segoe UI", FontWeight.BOLD, 18));
                lbl.setStyle("-fx-text-fill: black");

                //lbl.setTextFill(isSelected()?Color.WHITE: Color.BLACK);
                if (isSelected())  // This is always false :(
                   lbl.setStyle("-fx-text-fill: yellow");

                if (Rooms.getBoolean(item, "OwnerStatus")) {
                    lbl.setEffect(new DropShadow(15, Color.BLUEVIOLET));
                    lbl.setGraphic(new ImageView(
                                    new Image(getClass().getResourceAsStream("images/universal.png"))));
                } else  {
                    lbl.setGraphic(new ImageView(
                                    new Image(getClass().getResourceAsStream("images/yin-yang.png"))));
                    lbl.setEffect(new DropShadow(15, Color.WHITE));
                } 
                setGraphic(lbl); 

            }
        }
    }

1 个答案:

答案 0 :(得分:5)

-fx-selection-bar-text是根默认CSS选择器中定义的调色板(不是css属性),它是Scene的选择器。我不知道你是如何使用它的,但如果你定义它(全局,因为它是场景的选择器),如:

.root{
    -fx-selection-bar-text: red;
}

在您的CSS文件中,然后使用-fx-selection-bar-text的所有控件的css属性都将为红色。 ListView也会受到影响(请参阅下面的原始用法评论) 但是,如果您只想自定义ListView的样式,请以这种方式覆盖默认属性
(注意:仅覆盖-fx-text-fill。原始值已注释掉,其中使用了-fx-selection-bar-text

/* When the list-cell is selected and focused */
.list-view:focused .list-cell:filled:focused:selected {
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar;
    -fx-background-insets: 0, 1, 2;
    -fx-background: -fx-accent;
    /* -fx-text-fill: -fx-selection-bar-text;  */
    -fx-text-fill: red;
}

/* When the list-cell is selected and selected-hovered but not focused. 
    Applied when the multiple items are selected but not focused */
.list-view:focused .list-cell:filled:selected, .list-view:focused .list-cell:filled:selected:hover {
    -fx-background: -fx-accent;
    -fx-background-color: -fx-selection-bar;
    /* -fx-text-fill: -fx-selection-bar-text;  */
    -fx-text-fill: green;
}

/* When the list-cell is selected, focused and mouse hovered */
.list-view:focused .list-cell:filled:focused:selected:hover {
    -fx-background: -fx-accent;
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar;
    -fx-background-insets: 0, 1, 2;
    /* -fx-text-fill: -fx-selection-bar-text;  */
    -fx-text-fill: yellow;
}

内置 caspian.css 可以使用这些CSS属性和更多属性。


更新:我强烈建议您阅读Cell API。从那里

  

...我们仅使用极少数单元表示非常大的数据集。   每个Cell都被“回收”或重复使用。

警告不同的String项可能使用相同的单元格,以误导性的视觉效果/渲染结束,例如代码中的isSelected()。另外在API中它说

  

因为到目前为止,单元格最常见的用例是向a显示文本   用户,此用例专门针对Cell内部进行了优化。这是   由Labled扩展的Cell完成。这意味着子类   Cell只需要设置text属性,而不是单独创建   在Cell中标记并设置它。

所以我重构了你的代码如下。

class RoomCell extends ListCell<String> {
    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (item != null) {
            Log.debug("RoomCell called, item: "+item);
            setFont(Font.font("Segoe UI", FontWeight.BOLD, 18));
            ImageView iView = new ImageView();
            if (Rooms.getBoolean(item, "OwnerStatus")) {
                iView.setEffect(new DropShadow(15, Color.BLUEVIOLET));
                iView.setImage(new Image(getClass().getResourceAsStream("images/universal.png")));
            } else  {
                iView.setEffect(new DropShadow(15, Color.WHITE));
                iView.setImage(new Image(getClass().getResourceAsStream("images/yin-yang.png")));
            } 
            setGraphic(iView);  // The image will be displayed here
            setText(item); // The room name will be displayed here
        }
    }
}

单元格文本的所有-fx-text-fill样式都将根据CSS文件中的定义进行更改。

现在这里是单元格文本阴影效果与CSS文件填充颜色之间的权衡:
  - 如果你想使用drophadow效果,你应该像当前的方式,即创建标签,设置其文本,给标签和setGraphic(标签)赋予dorpshadow效果。但是这次你不想设置单元格的文本(setText(item)),因此CSS文件中的文本颜色样式将不起作用。
  - 另一方面,如果您更喜欢我重构的代码,那么您应该通过将其设置为-fx-background-color或{{Labeled来禁用单元格transparent。 1}}并在CSS文件中将null设置为dropshadow,以便能够直接将Drophadow效果应用于文本。清除单元格的背景不是IMO的首选方式。代码解释:

-fx-effect

测试它们以查看差异。就是这样。

相关问题