JavaFX TableColumn text wrapping

I am having a problem resizing a TableView table containing text elements that wrap around TableCell elements. When you resize, hidden values ​​change, but visible elements do not recount text wrapping.

The issue.

The tweets in the red box were hidden during the resize and were adjusted as expected. Tweets above the field were visible during the resize phase and still have the old packaging.

Below is my code for the resize phase.

fxSearchResultsTableTweet.setCellFactory(new Callback<TableColumn<Status, String>, TableCell<Status, String>>() { @Override public TableCell<Status, String> call(TableColumn<Status, String> arg0) { return new TableCell<Status, String>() { private Text text; @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (!isEmpty()) { text = new Text(item.toString()); text.setWrappingWidth(fxSearchResultsTableTweet.getWidth()); this.setWrapText(true); setGraphic(text); } } }; } }); } 

Any help would be greatly appreciated.

+11
java javafx javafx-2 tableview
source share
2 answers

This is closer, but not very good:

  textCol.setCellFactory(new Callback<TableColumn<Status, String>, TableCell<String, String>>() { @Override public TableCell<Status, String> call( TableColumn<Status, String> param) { TableCell<Status, String> cell = new TableCell<>(); Text text = new Text(); cell.setGraphic(text); cell.setPrefHeight(Control.USE_COMPUTED_SIZE); text.wrappingWidthProperty().bind(cell.widthProperty()); text.textProperty().bind(cell.itemProperty()); return cell ; } }); 

In 2.2, this displays the wrong height when you add new elements to the table, and then when you resize the cells are sorted correctly. In 8 it is almost perfect, it seems that it failed after adding the first element (at least in my layout).

As noted in the comments,

 textCol.setCellFactory(tc -> { TableCell<Status, String> cell = new TableCell<>(); Text text = new Text(); cell.setGraphic(text); cell.setPrefHeight(Control.USE_COMPUTED_SIZE); text.wrappingWidthProperty().bind(textCol.widthProperty()); text.textProperty().bind(cell.itemProperty()); return cell ; }); 

seems to work better.

+15
source share

Just add a cell factory to each column table. This should be added before adding your data to the table view. This works well for me.

  yourTableColumn.setCellFactory(param -> { return new TableCell<YourDataClass, String>() { @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setText(null); setStyle(""); } else { Text text = new Text(item); text.setStyle("-fx-text-alignment:justify;"); text.wrappingWidthProperty().bind(getTableColumn().widthProperty().subtract(35)); setGraphic(text); } } }; }); 
0
source share

All Articles