2016-05-29 6 views
0

Ich habe versucht, Rich-Texte in Zellen von TableView anzuzeigen, und ich habe meine eigene Unterklasse von TableCell erstellt und ein TextFlow-Objekt als Grafik festgelegt. Aber der TextFlow hat eine zusätzliche Höhe wie diese (TextFlow ist in der rechten Spalte): TextFlow has an extra height Warum passiert es?TextFlow in TableCell hat eine zusätzliche Höhe

Der Code folgt (Ich schreibe es in Groovy, aber es ist fast das gleiche wie 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 
    } 

} 
+0

Eine Lösung kann diesen Code ersetzen muss hier: https://stackoverflow.com/questions/42855724/textflow -inside-tablecell-not-correct-Zellenhöhe/47832905 # 47832905 – adrianopol

Antwort

0

Ich weiß nicht, warum Säulenhöhe nicht korrekt berechnet werden? Aber Sie können das Problem lösen, wenn Sie TextFlow durch FlowPane ersetzen.

Wenn Sie Text wickeln möchten, können Sie

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

von

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