快照整个TableView

问题描述:

我目前正在尝试使用快照从 TableView 创建图像,并且快照仅捕获屏幕上可见的tableview.我尝试弄乱 SnapshotPreferences viewport,但这没有帮助.任何建议或解决方法,将不胜感激.

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.

我现在也尝试创建一个宽度和高度与 TableView 相同的宽度和高度的新窗格,添加 TableView ,并对其进行快照,但是它最终空白.我继续玩这个游戏.创建一个新窗格并将其添加到场景中后,将为我提供 TableView 的所有列,但不是所有行.

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.

拍摄快照的代码如下.

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);

好,所以我想出了一种解决方法.我使用了 TableView 的基础数据,并制作了一个 GridPane 来表示基础数据,效果很好.我的数据用颜色表示,因此您将在下面看到.您当然可以对其他数据类型执行此操作.

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.

现在我在改变网格的阴影和宽度时遇到了麻烦,但这是另一个问题.创建 GridPane 的代码如下.

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);