我在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;
}
};
});
}https://stackoverflow.com/questions/50617029
复制相似问题