[英]Get unfiltered Java FX TableView columns
I have a TableView with rows that are filtered depending on what you type in a TextField which searches all the columns , using this piece of code : 我有一个TableView,其中的行根据您在TextField中键入的内容进行过滤,该文本使用以下代码搜索所有列:
FilteredList<CommandeFX> filteredData = new FilteredList<>(data, p -> true);
// 2. Set the filter Predicate whenever the filter changes.
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(commande -> {
// If filter text is empty, display all persons.
if (newValue == null || newValue.isEmpty()) {
return true;
}
// Compare first name and last name of every person with filter text.
String lowerCaseFilter = newValue.toLowerCase();
if (String.valueOf(commande.getCodeCommande()).toLowerCase().contains(lowerCaseFilter)) {
return true;
// Filter matches first name.
} else if (String.valueOf(commande.getCMD()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getClient()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getFournisseur()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getQuantite()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getPrixCommande()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getSommeProperty()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getResteCommandeProperty()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getDateCommande()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getDateReception()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getModified()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
else if (String.valueOf(commande.getValide()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
return false; // Does not match.
});
});
// 3. Wrap the FilteredList in a SortedList.
SortedList<CommandeFX> sortedData = new SortedList<>(filteredData);
// 4. Bind the SortedList comparator to the TableView comparator.
sortedData.comparatorProperty().bind(tableCommandes.comparatorProperty());
// 5. Add sorted (and filtered) data to the table.
tableCommandes.setItems(sortedData);
No problem so far, I also use these lines of code to change the styling (color in my case) of certain columns depending on a column value ( the column is titled "reste") : 到目前为止没有问题,我还使用这些代码行根据列值(该列的标题为“ reste”)来更改某些列的样式(在我的情况下为颜色):
reste.setCellFactory(column -> {
return new TableCell<CommandeFX, Number>() {
@Override
protected void updateItem(Number item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow<BonFX> currentRow = getTableRow();
if (!isEmpty()) {
if(Float.valueOf((float) item)>0)
currentRow.setStyle("-fx-background-color:lightcoral");
}
}
};
});
When I filter the rows, say I had three rows all colored in red, after filtering I have one row left displayed and colored in red, great ! 当我过滤行时,假设我有三行都用红色上色,过滤后我剩下了显示的一行并用红色上色,太棒了! The problem is the two invisible rows are still shown in red although they're not displayed! 问题是尽管没有显示两个不可见的行,它们仍然显示为红色! How can disable styling rows that are visible anymore ? 如何禁用样式可见的样式行? Thank you so much 非常感谢
If your row is empty then set its default color: 如果您的行为空,则设置其默认颜色:
reste.setCellFactory(column -> {
return new TableCell<CommandeFX, Number>() {
@Override
protected void updateItem(Number item, boolean empty) {
super.updateItem(item, empty);
TableRow<BonFX> currentRow = getTableRow();
if (empty || item == null) {
setText("");
setGraphic(null);
currentRow.setStyle("-fx-background-color:(****your default color****)");
} else {
setText(getItem().toString());
}
if (!isEmpty()) {
if (Float.valueOf((float) item) > 0) {
currentRow.setStyle("-fx-background-color:lightcoral");
}
}
}
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.