使用TextFlow突出显示TableView中的文本

时间:2014-05-22 22:27:35

标签: javafx tableview highlight textflow

我从JavaFX 8开始阅读,您可以使用TextFlow来突出显示文本。但我不知道如何将它用于我的TableView。在我的控制器类中,我有这个:

TableView<Person> tvPerson;
TableColumn<Person, String> tcName;
ObservableList<Person> personList;

tcName.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
tvPerson.setItems(personList);

这是内容类:

public class Person {
    private final SimpleStringProperty name = new SimpleStringProperty("");

    public Person(String name) {
        setName(name);
    }

    public String getName() {
        return name.getValue();
    }
    public void setName(String t) {
        name.set(t);
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

这有点晚了,但这是我的解决方案(确定它不是&#34;最佳实践&#34;,但对我有用,到目前为止还没有找到更好的方法如果有钢需要它。它只是一些部分:我认为在这种情况下它更容易理解。请注意,该列不可编辑。为了使其可编辑,你需要owervrite方法&#34; startEdit&#34;,&#34; cancelEdit&#34;和#34; commitEdit&#34; in&#34;返回新的TableCell()&#34; (它是出于这个问题的主题)。

    @FXML private TableView<YourEntity> table;

    @Override
    @SuppressWarnings("unchecked")
    public void initialize(URL location, ResourceBundle resources) { 

        TableColumn column1 = new TableColumn(resources.getString("YourShowedColumnName"));
        column1.setCellValueFactory(new PropertyValueFactory<>("YourDBColumnName"));
        table.getColumns().setAll(column1, ..., .......);
        table.setFixedCellSize(20.0); //To not change it in "graphic" with TextFlow

    column1.setCellFactory(column -> {
            return new TableCell<Mandant, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item == null || empty) {
                        setGraphic(null);
                        setText(null);
                        setStyle("");
                    } else {        
                        setGraphic(null);                       
                        if (!searchField.getText().isEmpty() && item.toLowerCase().contains(searchField.getText().toLowerCase())) {
                            Double rowHeight = this.getTableRow().getHeight();
                            setGraphic(buildTextFlow(item, searchField.getText()));
                            setHeight(rowHeight);
                            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        } else {
                            setText(item);
                            setTextFill(Color.BLACK);
                            setStyle("");
                            setContentDisplay(ContentDisplay.TEXT_ONLY);
                        }   
                    }
                }
            };

table.setEditable(true);

    /**
 * Build TextFlow with selected text. Return "case" dependent.
 * 
 * @param text - string with text
 * @param filter - string to select in text
 * @return - TextFlow
 */
private TextFlow buildTextFlow(String text, String filter) {
    int filterIndex = text.toLowerCase().indexOf(filter.toLowerCase());
    Text textBefore = new Text(text.substring(0, filterIndex));
    Text textAfter = new Text(text.substring(filterIndex + filter.length()));
    Text textFilter = new Text(text.substring(filterIndex,  filterIndex + filter.length())); //instead of "filter" to keep "case"
    textFilter.setFill(Color.ORANGE);
    textFilter.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
    return new TextFlow(textBefore, textFilter, textAfter);
}

希望对某人有所帮助。

enter image description here