I tried to display rich texts in cells of TableView, and I created my own subclass of TableCell and set a TextFlow object as a graphic. But the TextFlow has an extra height like this (TextFlow is in the right column):
Why does it happen?
The code is following (I wrote it in Groovy but it is almost the same as in Java):
public class ContentCell extends TableCell<Word, WordContent> {
protected void updateItem(WordContent wordContent, boolean isEmpty) {
super.updateItem(wordContent, isEmpty)
if (isEmpty || wordContent == null) {
setText(null)
setGraphic(null)
} else {
TextFlow textFlow = wordContent.getContentTextFlow()
setText(null)
setGraphic(textFlow)
}
}
}
public class DictionaryController implements Initializable {
@FXML private TableView<Word> wordTableView
@FXML private TableColumn<Word, String> nameColumn
@FXML private TableColumn<Word, WordContent> contentColumn
public void initialize(URL location, ResourceBundle resources) {
contentColumn.setCellFactory() { TableColumn<Word, WordContent> column ->
return new ContentCell()
}
for (int i : 0 ..< 50) {
WordContent wordContent = new WordContent("test")
Word word = new Word("test" + i.toString(), wordContent)
wordTableView.getItems().add(word)
}
}
}
public class WordContent {
private TextFlow contentTextFlow = new TextFlow()
public WordContent(String content) {
createContentTextFlow(content)
}
private void createContentTextFlow(String content) {
Text contentText = new Text(content)
contentTextFlow.getChildren().addAll(contentText)
}
public TextFlow getContentTextFlow() {
return contentTextFlow
}
}
public class Word {
private StringProperty name = new SimpleStringProperty()
private ObjectProperty<WordContent> content = new SimpleObjectProperty()
public Word(String name, WordContent content) {
this.name.set(name)
this.content.set(content)
}
public StringProperty nameProperty() {
return name
}
public ObjectProperty<WordContent> contentProperty() {
return content
}
}