2

I am currently trying to create an image from a TableView using snapshot, and the snapshot is only grabbing the tableview that is visible on the screen. I've tried messing with the SnapshotPreferencesviewport, but that didn't help. Any suggestions or workarounds would be appreciated.

EDIT: I have now also tried creating a new pane of the same width and height of the TableView, adding the TableView, and taking a snapshot of that, but it ends up blank. I've continued to play around with this. Creating a new pane and adding it to a scene gives me all the columns of the TableView, but not all the rows.

Code to take the snapshot is below.

WritableImage image = null;

//Get the node
TableView<ObservableList<ObjectProperty<Item>>>pattern =  mainApp.getPVController().getPattern();

//Create rectangle to define viewport
Rectangle2D rect = new Rectangle2D(0, 0, pattern.getWidth(), pattern.getHeight());

//Define snapshot parameters    
SnapshotParameters params = new SnapshotParameters();
params.setViewport(rect);

//Take the snapshot
image = pattern.snapshot(params, image);
Risky_91
  • 81
  • 3
  • 9

1 Answers1

1

Ok, so I figured out a workaround. I used the underlying data of the TableView and made a GridPane that represented the underlying data, which worked well. My data was represented by color, so that is what you will see below. You could certainly do this with other data types.

Now I am having trouble with the gridlines that I put in varying their shade and width- but that's another question. Code to create the GridPane is below.

 GridPane gPane = new GridPane();
     gPane.setSnapToPixel(true);
     gPane.setStyle("-fx-background-color: DARKGREY; -fx-padding: 1;"
                    +"-fx-hgap: 1; -fx-vgap: 1;");

     for(int i=0; i<mainApp.getItemList().size(); i++){ // rows
         for(int j=0; j<mainApp.getItemList().get(0).size(); j++){ //columns

             Color color = mainApp.getItemList().get(i).get(j).getValue().getDisplayColor();
                int r = (int) (color.getRed() * 255);
                int g = (int) (color.getGreen() * 255);
                int b = (int) (color.getBlue() * 255);

             Rectangle rect = new Rectangle(5,5);

             rect.setStyle("-fx-fill: rgb(" + r + "," + g + "," + b + ");");
             gPane.add(rect, j, i);
         }
     }
        Scene scene = new Scene(gPane);
        WritableImage image = gPane.snapshot(new SnapshotParameters(), null);
Risky_91
  • 81
  • 3
  • 9