当列为整数时,使用CSS隐藏表格视图中的单元格

时间:2017-07-25 15:10:39

标签: javafx

我有一个tableview,其中一列是:

private TableColumn<UserData, Integer> countListColumn;

我已经能够在除了这一列之外的所有列中使用CSS消除空单元格,因为这只是在每个单元格中显示为零。我尝试了以下内容:

.table-row-cell:empty {
    -fx-background-color: transparent;
}
.table-row-cell:empty {
    -fx-border-width: 0px;
}
.table-row-cell:null {
    -fx-background-color: transparent;
}
.table-row-cell:null {
    -fx-border-width: 0px;
}
.list-cell:empty {
    -fx-background-color: white;
}
.list-cell:empty {
    visibility:hidden;
}

这些似乎都不起作用,也没有约束力。有没有办法隐藏这些单元格或让它们响应我的CSS?以下是我的尝试。

     public class GuiController implements SubscriptionObserverIF {


    @FXML
    private ObservableList<List> list = FXCollections.observableArrayList();
    @FXML
    private TableView<Data> table;
    @FXML
    private TableColumn<Data, Integer> Column;
    @FXML
    private TableColumn<Data, String> Column2;

    @FXML
    private void initialize() throws Exception {

        table.setPlaceholder(new Label(" SELECT COLUMN FOR INFORMATION"));
        DataTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        //table1.setStyle("-fx-table-cell-border-color: transparent;");
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        //table.setFixedCellSize(50);
        //table.prefHeightProperty().bind(Bindings.size(DataTable.getItems()));
        //table.prefHeight(DataTable.getItems().size());
        // column.getStyleClass().add("style_tableview.css");


        column.setCellValueFactory(data -> data.getValue().columnProperty().asObject());
        column2.setCellValueFactory(data -> data.getValue().columnProperty().asString());

        column2.setCellFactory(factory -> new TableCell<Data, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (empty) {
                    //System.out.println("EMPTY");
                    //item = null;
                    //setStyle("-fx-border-width: 0px;");
                }else if (!empty){
                    System.out.println("column2 is NOT EMPTY");
                    System.out.println(item);
                    if(Integer.parseInt(item) == 0){
                        System.out.println("DEFINITELY ZERO");
                        setStyle("-fx-border-width: 0px;");
                        setStyle("-fx-background-color: transparent;");
                    }else{
                        System.out.println("column2 is NOT EMPTY AND NOT ZERO");
                        getStyleClass().clear();
                        //getStyleClass().addAll("darkTheme.css");
                        setStyle("-fx-background-color: NAVY;");
                        setStyle("-fx-border-width: 1000px;");
                        //setStyle(null);
                        column2.setCellValueFactory(data -> data.getValue().columnProperty().asString());

                    }
                }

            }
        });
//        column.setCellFactory(factory -> new TableCell<Data, Integer>() {
//            @Override
//            protected void updateItem(Integer item, boolean empty) {
//                super.updateItem(item, empty);
//                if(item instanceof Integer) {
//                        setText(Integer.toString((Integer) item));
//                        setText(null);
//                }
//                if (empty) {
//                    setText(null);
//                } else if (item == 0) {
//                    setText(null);
//                } else {
//                    setText(item.toString());
//                }
//            }
//        });
        statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
        timelineColumn.setCellValueFactory(cellData -> cellData.getValue().dataProperty());

        //IntStream.range(0, 20).mapToObj(Integer::toString).forEach(DataTable.getItems()::add);

//
//        DataTable.setFixedCellSize(25);
//        DataTable.prefHeightProperty().bind(DataTable.fixedCellSizeProperty().multiply(Bindings.size(DataTable.getItems()).add(1.01)));
//        DataTable.minHeightProperty().bind(DataTable.prefHeightProperty());
//        DataTable.maxHeightProperty().bind(DataTable.prefHeightProperty());
        timelineColumn.setCellFactory(column -> {
            return new TableCell<Data, ObservableList<dataData>>() {
                @Override
                protected void updateItem(ObservableList<data> item, boolean empty) {
                    super.updateItem(item, empty);

                    if(empty) {
                        setGraphic(null);

                    }
                    else {
                        this.autosize();
                        setGraphic(DataGraphic.createGraphic(item, DataTable.getHeight(), DataTable.getWidth(), position)););
//                      DataTable.setStyle("-fx-table-cell-border-color: WHITE;");
//                      DataTable.setFixedCellSize(25);
//                      DataTable.prefHeightProperty().bind(DataTable.fixedCellSizeProperty().multiply(Bindings.size(DataTable.getItems())));
                    }                };
        });



                }

1 个答案:

答案 0 :(得分:0)

我认为这是解决问题的方法:

countListColumn.setCellFactory(factory -> new TableCell<UserData, Integer>() {
        @Override
        protected void updateItem(Integer item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
            } else if (item == 0) {
                setText(null);
            } else {
                setText(item.toString());
            }
        }
    });

当然,你必须像这样设置你的cellValueFactory:

    countListColumn.setCellValueFactory(data -> data.getValue().countProperty().asObject());