2017-07-25 32 views
0

我有一个tableview中,其中一列是:隐藏单元格使用CSS时,柱是整数

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



       } 

回答

0

我认为这是对你的问题的sollution:

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()); 
+0

你更新代码的伟大工程使该列中的单元格不显示零,但不幸的是它仍然不响应CSS,因此我得到一个剩余的列,而其周围的列直到字符串数据填充时才显示,这是w我希望这个专栏也能发生。 –

+0

您是否尝试添加将css添加到'updateItem'方法内的'styleClass'? – Sunflame

+0

是的。实际上,我最终做的是创建了一个新的字符串列并复制整数数据用于显示目的,但现在列将根据CSS消失,但不会在数据生成时重新出现。 –