Guest User

Untitled

a guest
Nov 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package hellofx;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.TreeItem;
  8. import javafx.scene.control.TreeTableCell;
  9. import javafx.scene.control.TreeTableColumn;
  10. import javafx.scene.control.TreeTableView;
  11. import javafx.stage.Stage;
  12.  
  13. public class TreeTableMemoryLeak extends Application {
  14. public static void main(String[] args) {
  15. launch(args);
  16. }
  17.  
  18. @Override
  19. public void start(Stage stage) {
  20. TreeTableView<Object> otable = buildTableView();
  21. Platform.runLater(()->{
  22. Scene scene = new Scene(otable,otable.getWidth(),otable.getHeight());
  23. stage.setScene(scene);
  24. stage.show();
  25. });
  26. }
  27.  
  28. private TreeTableView<Object> buildTableView() {
  29. TreeTableView<Object> table = new TreeTableView<>();
  30. TreeTableColumn<Object, String> indexCol = new TreeTableColumn<>("Index");
  31. indexCol.setCellFactory(column -> {
  32. return new TreeTableCell<Object, String>() {
  33. @Override
  34. protected void updateItem(String item, boolean empty) {
  35. setText(String.valueOf(getIndex()));
  36. }
  37. };
  38. });
  39. TreeTableColumn<Object, String> dynCol = new TreeTableColumn<>("DYNAMIC");
  40. dynCol.setGraphic(new Button("X"){
  41. {
  42. //ATTENTION 3 (On click of X remove all columns except first column)
  43. setOnAction((e)->dynCol.getColumns().removeIf(c->c!=dynCol.getColumns().get(0)));
  44. }
  45. });
  46. //ATTENTION 1
  47. table.setTableMenuButtonVisible(true);
  48.  
  49. //ATTENTION 2 (39 is the magic number anything higher it will OOM on my device I have total 6G physical.)
  50. for(int i=0;i<39;i++){
  51. dynCol.getColumns().add(new TreeTableColumn<>(String.valueOf(i)));
  52. }
  53. table.getColumns().addAll(indexCol,dynCol);
  54. table.setEditable(true);
  55. table.setRoot(new TreeItem<Object>());
  56. return table;
  57. }
  58. }
Add Comment
Please, Sign In to add comment