【发布时间】:2018-05-31 05:11:24
【问题描述】:
我在 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;
}
};
});
}
【问题讨论】: