我需要在单元格中填充数据库中的文本,但是当单元格已满时,文本应该在下一个单元格中继续,向下。
我有一个想法是如何实现它的:
Text theText = new Text(jFXTextFieldSearch.getText());
theText.setFont(jFXTextFieldSearch.getFont());
double width = theText.getBoundsInLocal().getWidth();
System.out.println(width);
这种方法有多好还是有另一种方法可以做到这一点? 数据库足够大,有50页的表格。 附:对不起我的坏工程。(
答案 0 :(得分:0)
试试这个:
@FXML
private TableView<String> table;
@FXML
private TableColumn<String, String> column;
...
String string =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " +
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " +
"voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
"non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
AttributedString attributedString = new AttributedString(string);
attributedString.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD, 16));
AttributedCharacterIterator iterator = attributedString.getIterator();
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
ObservableList<String> list = FXCollections.observableArrayList();
while (measurer.getPosition() < iterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout((float) column.getWidth());
list.add(string.substring(measurer.getPosition() - layout.getCharacterCount(), measurer.getPosition()));
}
table.setItems(list);
要提交给attributedString
的字体应与用于在所需列中呈现的字体相同。