Out*_*ONE 0 java javafx filter tableview nstableviewcell
我已经用列制作了我的表格视图,一切正常。
我注意到的一件事是,当我过滤行时,一些数据保留在我定义为 cellFactory 的列中。
显示问题的图像:
列的代码:
@FXML
private TableColumn<Log, Integer> colQuanJour;
colQuanJour.setCellValueFactory(new PropertyValueFactory<>("quantite"));
Run Code Online (Sandbox Code Playgroud)
CellFactory 添加颜色和字符(+ 或 -)
colQuanJour.setCellFactory(
new Callback<TableColumn<Log,Integer>,TableCell<Log,Integer>>(){
@Override
public TableCell<Log, Integer> call(TableColumn<Log, Integer> param) {
return new TableCell<Log, Integer>(){
@Override
protected void updateItem(Integer item, boolean empty) {
if( ! empty){
int currIndex = indexProperty().getValue() < 0 ? 0
: indexProperty().getValue();
int operIndex = param.getTableView().getItems().get(currIndex).getOperation().getId_operation();
setStyle("-fx-font-weight: bold");
String Char ;
if(operIndex == 2){
setStyle("-fx-font-size : 16px");
setStyle("-fx-text-fill : red");
Char = "-";
}else {
setStyle("-fx-font-size : 16px");
setStyle("-fx-text-fill : green");
Char = "+";
}
String val = String.valueOf(param.getTableView().getItems().get(currIndex).getQuantite());
setText(Char+val);
}
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
Log 是一个包含属性数量 (SimpleIntegerProperty) 的 pojo。
我的桌子是
TableView<Log> tblart;
Run Code Online (Sandbox Code Playgroud)
过滤功能:
此函数从组合框中获取值以选择用于过滤的列,默认情况下它从所有表中过滤,如下所示:
void filterOneJour(String id,String newVal){
ObservableList<Log> obsList = FXCollections.observableArrayList();
for(Log jour : tblJour.getItems()){
String num = String.valueOf(jour.getNum());
String qte = String.valueOf(jour.
switch(id){
case "Numéro" :
if(num.toUpperCase().contains(newVal))
obsList.add(jour);break;
case "Quantité":
if(qte.toUpperCase().contains(newVal))
obsList.add(jour);break;
default :
if(num.toUpperCase().contains( qte.toUpperCase().contains(newVal) )
obsList.add(jour);
}
}
tblJour.setItems(obsList);
}
Run Code Online (Sandbox Code Playgroud)
所以我希望一切都清楚,我认为问题来自 cellFactory,更确切地说是 updateItem。
是的,正如您提到的,问题来自 updateItem 方法。TableView(实际上是 VirtualFlow)是通过使用相同的行/单元格来呈现的,以避免性能问题。将根据提供的项目调用行/单元格 updateItem。在您的情况下,您仅在项目存在时添加更新单元格,并在项目为空时忽略它。因此单元格不会为空行更新。
要解决您的问题.. 您需要添加else 条件。
colQuanJour.setCellFactory(
new Callback<TableColumn<Log,Integer>,TableCell<Log,Integer>>(){
@Override
public TableCell<Log, Integer> call(TableColumn<Log, Integer> param) {
return new TableCell<Log, Integer>(){
@Override
protected void updateItem(Integer item, boolean empty) {
if( ! empty){
// Your code...
setText(Char+val);
}else{
setText(null); // This will clear the text for empty rows
}
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |