为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 TableView (JavaFX11) 拆分为 n 个部分以保存为 png

如何将 TableView (JavaFX11) 拆分为 n 个部分以保存为 png

天涯尽头无女友 2022-07-20 09:59:12
问题:我想保存一个包含 n 个部分的 TableView 数据的文档。该函数应每隔 10 个元素拆分表,并将每 10 个元素保存在干净的表中。我已经找到了一个分页示例并尝试使用模 % 运算符,但它的行为对我来说很奇怪。私人无效打印文档(){createDemoData();   // creates a set of 20 rowsStage stage = (Stage) tableView.getScene().getWindow();TableView<Receipt> tempTableView = tableView; // copy tableView to tempTableViewint size = tempTableView.getItems().size();tableView.getItems().removeAll();tableView.refresh();for(int i=1; i<size; i++) {     tableView.getItems().set(i,tempTableView.getItems().get(i));    if (i % 10 == 0)    {                // params: filename, fxml node        saveAsPng("page" + i, stage.getScene().lookup("#doc"));        tableView.getItems().removeAll();        tableView.refresh();    } }saveAsPng() 函数只是使用了 javafx 的快照函数。两个 png 文件的输出如下所示:显示 TableView 的输出图像两次都有表中的所有 20 个元素。但我想让它在单独的表格文件中每隔 10 个元素拆分一次,以便在多页上打印具有准确外观的页眉、页脚的收据,并且在文档中间我想打印一个静态/固定尺寸的表格。
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

您可以尝试类似于下面的代码。代码打印TableView十个项目。然后它清除TableView并添加十个要打印的项目。它将继续此过程,直到打印完所有项目。


它打开打印对话框。这将允许您将打印件保存为 PDF 以查看它,而不是浪费纸张。


import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.print.PrinterJob;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TableColumn;

import javafx.scene.control.TableView;

import javafx.scene.control.cell.PropertyValueFactory;

import javafx.scene.layout.HBox;

import javafx.scene.layout.VBox;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.scene.text.FontWeight;

import javafx.scene.text.Text;

import javafx.stage.Stage;


public class FxTableViewExample1 extends Application

{


    private TableView<Book> table;

    private ObservableList<Book> data;

    private Text actionStatus;


    public static void main(String[] args)

    {


        Application.launch(args);

    }


    @Override

    public void start(Stage primaryStage)

    {


        primaryStage.setTitle("Table View Example 1");


        // Books label

        Label label = new Label("Books");

        label.setTextFill(Color.DARKBLUE);

        label.setFont(Font.font("Calibri", FontWeight.BOLD, 36));

        HBox hb = new HBox();

        hb.setAlignment(Pos.CENTER);

        hb.getChildren().add(label);


        // Table view, data, columns and properties

        table = new TableView();

        data = getInitialTableData();

        table.setItems(data);


        TableColumn titleCol = new TableColumn("Title");

        titleCol.setCellValueFactory(new PropertyValueFactory("title"));

        TableColumn authorCol = new TableColumn("Author");

        authorCol.setCellValueFactory(new PropertyValueFactory("author"));


        table.getColumns().setAll(titleCol, authorCol);

        table.setPrefWidth(450);

        table.setPrefHeight(300);

        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);


        // Status message text

        actionStatus = new Text();

        actionStatus.setFill(Color.FIREBRICK);


        Button button = new Button("Print");


        // Vbox

        VBox vbox = new VBox(20);

        vbox.setPadding(new Insets(25, 25, 25, 25));;

        vbox.getChildren().addAll(hb, table, actionStatus, button);


        // Scene

        Scene scene = new Scene(vbox, 500, 475); // w x h

        primaryStage.setScene(scene);

        primaryStage.show();


        // Select the first row

        table.getSelectionModel().select(0);

        Book book = table.getSelectionModel().getSelectedItem();

        actionStatus.setText(book.toString());


        List<List<Book>> bookLists = partition(data, 10);


        button.setOnAction((event) -> {

            PrinterJob printerJob = PrinterJob.createPrinterJob();

            printerJob.showPrintDialog(primaryStage);

            for (int i = 0; i < bookLists.size(); i++) {

                data.clear();

                data.addAll(bookLists.get(i));

                printerJob.printPage(table);

            }


            printerJob.endJob();


        });


    }


    private ObservableList getInitialTableData()

    {

        List list = new ArrayList();

        list.add(new Book("The Thief", "Fuminori Nakamura"));

        list.add(new Book("Of Human Bondage", "Somerset Maugham"));

        list.add(new Book("The Bluest Eye", "Toni Morrison"));

        list.add(new Book("I Am Ok You Are Ok", "Thomas Harris"));

        list.add(new Book("Magnificent Obsession", "Lloyd C Douglas"));

        list.add(new Book("100 Years of Solitude", "Gabriel Garcia Marquez"));

        list.add(new Book("What the Dog Saw", "Malcolm Gladwell"));

        list.add(new Book("The Fakir", "Ruzbeh Bharucha"));

        list.add(new Book("The Hobbit", "J.R.R. Tolkien"));

        list.add(new Book("Strange Life of Ivan Osokin", "P.D. Ouspensky"));

        list.add(new Book("The Hunt for Red October", "Tom Clancy"));

        list.add(new Book("Coma", "Robin Cook"));


        list.add(new Book("A Catskill Eagle", "xxx"));

        list.add(new Book("The Children of Men", "xxx"));

        list.add(new Book("Clouds of Witness", "xxx"));

        list.add(new Book("A Confederacy of Dunces", "xxx"));

        list.add(new Book("Consider Phlebas", "xxx"));

        list.add(new Book("Consider the Lilies", "xxx"));

        list.add(new Book("Cover Her Face", "xxx"));

        list.add(new Book("The Cricket on the Hearth", "xxx"));

        list.add(new Book("The Curious Incident of the Dog in the Night-Time", "xxx"));

        list.add(new Book("The Daffodil Sky", "xxx"));

        list.add(new Book("Dance Dance Dance", "xxx"));

        list.add(new Book("A Darkling Plain", "xxx"));


        list.add(new Book("The Thief", "Fuminori Nakamura"));

        list.add(new Book("Of Human Bondage", "Somerset Maugham"));

        list.add(new Book("The Bluest Eye", "Toni Morrison"));

        list.add(new Book("I Am Ok You Are Ok", "Thomas Harris"));

        list.add(new Book("Magnificent Obsession", "Lloyd C Douglas"));

        list.add(new Book("100 Years of Solitude", "Gabriel Garcia Marquez"));

        list.add(new Book("What the Dog Saw", "Malcolm Gladwell"));

        list.add(new Book("The Fakir", "Ruzbeh Bharucha"));

        list.add(new Book("The Hobbit", "J.R.R. Tolkien"));

        list.add(new Book("Strange Life of Ivan Osokin", "P.D. Ouspensky"));

        list.add(new Book("The Hunt for Red October", "Tom Clancy"));

        list.add(new Book("Coma", "Robin Cook"));


        list.add(new Book("A Catskill Eagle", "xxx"));

        list.add(new Book("The Children of Men", "xxx"));

        list.add(new Book("Clouds of Witness", "xxx"));

        list.add(new Book("A Confederacy of Dunces", "xxx"));

        list.add(new Book("Consider Phlebas", "xxx"));

        list.add(new Book("Consider the Lilies", "xxx"));

        list.add(new Book("Cover Her Face", "xxx"));

        list.add(new Book("The Cricket on the Hearth", "xxx"));

        list.add(new Book("The Curious Incident of the Dog in the Night-Time", "xxx"));

        list.add(new Book("The Daffodil Sky", "xxx"));

        list.add(new Book("Dance Dance Dance", "xxx"));

        list.add(new Book("A Darkling Plain", "xxx"));


        return FXCollections.observableList(list);

    }


    private static <T> List<List<T>> partition(Collection<T> members, int maxSize)

    {

        List<List<T>> res = new ArrayList<>();


        List<T> internal = new ArrayList<>();


        for (T member : members) {

            internal.add(member);


            if (internal.size() == maxSize) {

                res.add(internal);

                internal = new ArrayList<>();

            }

        }

        if (internal.isEmpty() == false) {

            res.add(internal);

        }

        return res;

    }

}



查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信