Skip to content

Instantly share code, notes, and snippets.

@thelmstedt
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thelmstedt/2432b188aff575cff106 to your computer and use it in GitHub Desktop.
Save thelmstedt/2432b188aff575cff106 to your computer and use it in GitHub Desktop.
JavaFX8 bug - wrapped cells in a TableView don't scroll correctly
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.Collections;
import java.util.Map;
public class TableViewWrappingBug extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws InterruptedException {
ObservableList<Map> data = FXCollections.observableArrayList();
TableView<Map> tableView = buildTableView(data);
setTheScene(stage, tableView);
new Thread((() -> {
System.out.println("adding data");
for (int i = 0; i < 8; i++) {
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
data.add(Collections.singletonMap("A", i + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."));
}
})).start();
}
private TableView<Map> buildTableView(ObservableList<Map> data) {
TableView<Map> tableView = new TableView<>(data);
TableColumn<Map, String> col = new TableColumn<>("eg");
col.setCellValueFactory(new MapValueFactory<>("A"));
col.setMinWidth(200);
col.setCellFactory(ignored -> new TableCell<Map, String>() {
@Override
protected void updateItem(String item, boolean empty) {
if (item == null || empty) {
setGraphic(null);
setText(null);
} else {
setText(null);
Text value = new Text(item);
value.wrappingWidthProperty().bind(widthProperty());
setGraphic(value);
}
super.updateItem(item, empty);
}
});
tableView.getColumns().add(col);
return tableView;
}
private void setTheScene(Stage stage, TableView<Map> tableView) {
Scene scene = new Scene(new Group());
stage.setTitle("Click Button for bug");
stage.setWidth(300);
stage.setHeight(500);
BorderPane bp = new BorderPane();
bp.setCenter(tableView);
scene.setRoot(bp);
stage.setScene(scene);
stage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment