1

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): TextFlow has an extra height 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
  }

}
Ziphil
  • 59
  • 8
  • A solution can be found here: https://stackoverflow.com/questions/42855724/textflow-inside-tablecell-not-correct-cell-height/47832905#47832905 – adrianopol Dec 15 '17 at 13:04

1 Answers1

1

I don't know why column height is not calculate correctly? But you can resolve the problem if you replace TextFlow by FlowPane.

If you want to wrap text, you must replace this code

private void createContentTextFlow(String content) {
Text contentText = new Text(content)
contentTextFlow.getChildren().addAll(contentText)
}

by

private void createContentTextFlow(String content) {
Text contentText = new Text(content)
contentText.wrappingWidthProperty().bind(#your_column_object#.widthProperty().subtract(5))
contentTextFlow.getChildren().addAll(contentText)
}