2016-05-29 12 views
0

我試圖在TableView的單元格中顯示豐富的文本,並創建了我自己的TableCell的子類並將TextFlow對象設置爲圖形。但TextFlow具有這樣的額外高度(TextFlow在右欄中): TextFlow has an extra height 爲什麼會發生?TableCell中的TextFlow有一個額外的高度

的代碼下面(我寫在Groovy,但它幾乎是一樣的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 
    } 

} 
+0

溶液替換此代碼

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

可以在這裏找到:https://stackoverflow.com/questions/42855724/textflow - 內 - TableCell的 - 不正確的細胞高度/ 47832905#47832905 – adrianopol

回答

0

我不知道爲什麼柱高度不能正確計算?但是,如果您將TextFlow替換爲FlowPane,則可以解決該問題。

如果你想換行,你必須

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