我在tableview中搜索一个单词,有一个名为find的文本字段在tableview中查找任何文本然后我想用javafx tableview中的其他文本替换该文本?我已经运行了用于查找单词的代码,但现在我想要替换代码,任何人都可以帮我解决这个问题吗?
public void onClickSearch(ActionEvent event) {
String st=srep.getText();
//sbtn.setOnAction((ActionEvent event1) -> {
tc_source.setCellFactory((column) -> {
return new TableCell<File, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
setText(null);
setStyle("");
}
else {
if (!srep.getText().isEmpty() && item.toLowerCase().contains(srep.getText().toLowerCase())) {
Double rowHeight = this.getTableRow().getHeight();
setGraphic(buildTextFlow(item ,srep.getText()));
setHeight(rowHeight);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
else {
setText(item);
setTextFill(javafx.scene.paint.Color.BLACK);
setStyle("");
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
public TextFlow buildTextFlow(String text, String filter) {
int filterLength = filter.length();
if (filterLength == 0) {
return new TextFlow(createNormalText(text));
} Font font = Font.font("Italic", 12);
java.awt.Color color= java.awt.Color.BLUE ;
TextFlow t = new TextFlow();
String lowerText = text.toLowerCase();
filter = filter.toLowerCase();
int index1 = 0;
int matchIndex=0;
while ((matchIndex = lowerText.indexOf(filter, index1)) != -1) {
if (index1 != matchIndex) {
t.getChildren().add(createNormText(text.substring(index1, matchIndex)));
}
index1 = matchIndex + filterLength;
t.getChildren().add(createText(text.substring(matchIndex, index1), color));
}
if (index1 != text.length()) {
t.getChildren().add(createNormText(text.substring(index1)));
}
return t;
}
};
});
}
答案 0 :(得分:0)
替换cellFactory
不会自动导致列的单元格被新工厂替换。 afaik没有办法迫使TableView
重新创建它的细胞。您的初始cellFactory
返回的单元格应根据搜索文本动态更改:
private final StringProperty searchText = new SimpleStringProperty();
TableView
创建/初始化tc_source.setCellFactory(column -> new TableCell<File, String>() {
private final ChangeListener<String> listener = (o, oldValue, newValue) -> {
updateContents(getItem(), newValue);
};
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
searchText.removeListener(listener);
if (item == null || empty) {
setGraphic(null);
setText(null);
} else {
updateContents(item, searchText.get());
searchText.addListener(listener);
}
}
private void updateContents(String item, String search) {
if (search == null || search.isEmpty() || !item.toLowerCase().contains(search.toLowerCase())) {
setText(item);
setTextFill(javafx.scene.paint.Color.BLACK);
setContentDisplay(ContentDisplay.TEXT_ONLY);
} else {
double rowHeight = this.getTableRow().getHeight();
setGraphic(buildTextFlow(item, search));
setHeight(rowHeight);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
private TextFlow buildTextFlow(String text, String filter) {
int filterLength = filter.length();
if (filterLength == 0) {
return new TextFlow(createNormalText(text));
} Font font = Font.font("Italic", 12);
java.awt.Color color= java.awt.Color.BLUE; // why are we using the awt class here?
TextFlow t = new TextFlow();
String lowerText = text.toLowerCase();
filter = filter.toLowerCase();
int index1 = 0;
int matchIndex=0;
while ((matchIndex = lowerText.indexOf(filter, index1)) != -1) {
if (index1 != matchIndex) {
t.getChildren().add(createNormText(text.substring(index1, matchIndex)));
}
index1 = matchIndex + filterLength;
t.getChildren().add(createText(text.substring(matchIndex, index1), color));
}
if (index1 != text.length()) {
t.getChildren().add(createNormText(text.substring(index1)));
}
return t;
}
});
public void onClickSearch(ActionEvent event) {
searchText.set(srep.getText());
}
注意:您应该考虑重复使用TextFlow
,因为每次搜索或滚动到TextFlow
时,都会为当前代码中的每个单元格重新创建所有子项。