简体   繁体   English

如何将TableCell样式类绑定到TableRow样式类(JavaFX)

[英]How do I bind the TableCell style classes to the TableRow style classes (JavaFX)

I am making a system log window for my JavaFX application. 我正在为我的JavaFX应用程序创建系统日志窗口。 I figure the easiest way to display the log and be able to sort filter etc is to use a TableView. 我认为显示日志并能够对过滤器进行排序的最简单方法是使用TableView。

The log messages have four types that are classified by an enum(LogType): 日志消息具有按枚举(LogType)分类的四种类型:

SUCCESS, INFO, WARNING, ERROR. 成功,信息,警告,错误。

If the type is ERROR, I want to change the text of the entire TableRow to red using a CSS class .redText 如果类型为ERROR,我想使用CSS类.redText将整个TableRow的文本更改为红色。

I did this using the RowFactory. 我使用RowFactory做到了这一点。 Here is declaring the variables: 这里是声明变量:

@FXML
TableView<LogMessage> table;
@FXML
TableColumn<LogMessage, String> timeColumn;
@FXML
TableColumn<LogMessage, LogMessage.LogType> typeColumn;
@FXML
TableColumn<LogMessage, String> msgColumn;

Here is setRowFactory(). 这是setRowFactory()。 Its located in my initialize() method of the controller class. 它位于我的控制器类的initialize()方法中。

        //Change text color based on success/failure
        table.setRowFactory(new Callback<TableView<LogMessage>, TableRow<LogMessage>>() {

            @Override
            public TableRow<LogMessage> call(TableView<LogMessage> param) {
                TableRow<LogMessage> tableRow = new TableRow<LogMessage>() {

                    @Override
                    protected void updateItem(LogMessage item, boolean empty) {
                        super.updateItem(item, empty);
                        this.getStyleClass().remove("redText");
                        if (item != null && !empty) {
                            if(item.isError()) {
                                this.getStyleClass().add("redText");
                            }
                        }
                    }

                };

                return tableRow;
            }
        });

This works, except I've run into the issue where some of the log messages are too long for my fixed size table, so the user would have to horizontally scroll. 这行得通,除了我遇到了一些日志消息对于我的固定大小表来说太长的问题,因此用户必须水平滚动。

I would rather it wrap onto multiple lines. 我希望将其包装成多行。 So I added a custom cell factory on msgColumn , using some code I found on StackOverflow, like so: 因此,我使用了在StackOverflow上找到的一些代码在msgColumn上添加了一个自定义单元工厂,如下所示:

//Auto wrap the message column
        msgColumn.setCellFactory(tc -> {
            TableCell<LogMessage, String> cell = new TableCell<>();
            Text text = new Text();
            cell.setGraphic(text);
            cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
            text.wrappingWidthProperty().bind(msgColumn.widthProperty());
            text.textProperty().bind(cell.itemProperty());
            text.styleProperty().bind(cell.styleProperty());
            return cell ;
        });

I added the text.styleProperty().bind(cell.styleProperty()); 我添加了text.styleProperty().bind(cell.styleProperty()); to try to get it to copy over the style too, but no luck. 试图使它也能复制样式,但是没有运气。

I think the cellFactory for the message column is overwriting the style on the TableRow. 我认为消息列的cellFactory正在覆盖TableRow上的样式。 The word wrapping works great, but it has the default color as black for the msg column, and its red for the rest of the columns in that row. 自动换行效果很好,但味精列的默认颜色为黑色,该行中其余列的默认颜色为红色。

How can I get it to do both, word wrap and keep the style? 我该如何兼顾文字换行和保持样式?

I am open to doing it either through CSS or the Java Controller. 我愿意通过CSS或Java控制器来做到这一点。

Add a (fixed) style class to the Text : Text添加一个(固定的)样式类:

    msgColumn.setCellFactory(tc -> {
        TableCell<LogMessage, String> cell = new TableCell<>();
        Text text = new Text();
        cell.setGraphic(text);
        cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
        text.wrappingWidthProperty().bind(msgColumn.widthProperty());
        text.textProperty().bind(cell.itemProperty());
        text.getStyleClass().add("table-cell-text");
        return cell ;
    });

and then in your CSS you can just do, for example, 然后在CSS中,例如,

.redText .table-cell .table-cell-text {
    -fx-fill: red ;
}

Note that the CSS property for a Text object is -fx-fill . 请注意, Text对象的CSS属性是-fx-fill If you use a Label (or just want to set the text color of a table cell directly), you would use -fx-text-fill . 如果使用Label (或者只是想直接设置表格单元格的文本颜色),则可以使用-fx-text-fill

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM