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

问题:我想保存一个包含 n 个部分的 TableView 数据的文档。该函数应每隔 10 个元素拆分表,并将每 10 个元素保存在干净的表中。我已经找到了一个分页示例并尝试使用模 % 运算符,但它的行为对我来说很奇怪。


私人无效打印文档(){


createDemoData();   // creates a set of 20 rows


Stage stage = (Stage) tableView.getScene().getWindow();

TableView<Receipt> tempTableView = tableView; // copy tableView to tempTableView


int 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 个元素拆分一次,以便在多页上打印具有准确外观的页眉、页脚的收据,并且在文档中间我想打印一个静态/固定尺寸的表格。


天涯尽头无女友
浏览 122回答 1
1回答

至尊宝的传说

您可以尝试类似于下面的代码。代码打印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{&nbsp; &nbsp; private TableView<Book> table;&nbsp; &nbsp; private ObservableList<Book> data;&nbsp; &nbsp; private Text actionStatus;&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Application.launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Table View Example 1");&nbsp; &nbsp; &nbsp; &nbsp; // Books label&nbsp; &nbsp; &nbsp; &nbsp; Label label = new Label("Books");&nbsp; &nbsp; &nbsp; &nbsp; label.setTextFill(Color.DARKBLUE);&nbsp; &nbsp; &nbsp; &nbsp; label.setFont(Font.font("Calibri", FontWeight.BOLD, 36));&nbsp; &nbsp; &nbsp; &nbsp; HBox hb = new HBox();&nbsp; &nbsp; &nbsp; &nbsp; hb.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; hb.getChildren().add(label);&nbsp; &nbsp; &nbsp; &nbsp; // Table view, data, columns and properties&nbsp; &nbsp; &nbsp; &nbsp; table = new TableView();&nbsp; &nbsp; &nbsp; &nbsp; data = getInitialTableData();&nbsp; &nbsp; &nbsp; &nbsp; table.setItems(data);&nbsp; &nbsp; &nbsp; &nbsp; TableColumn titleCol = new TableColumn("Title");&nbsp; &nbsp; &nbsp; &nbsp; titleCol.setCellValueFactory(new PropertyValueFactory("title"));&nbsp; &nbsp; &nbsp; &nbsp; TableColumn authorCol = new TableColumn("Author");&nbsp; &nbsp; &nbsp; &nbsp; authorCol.setCellValueFactory(new PropertyValueFactory("author"));&nbsp; &nbsp; &nbsp; &nbsp; table.getColumns().setAll(titleCol, authorCol);&nbsp; &nbsp; &nbsp; &nbsp; table.setPrefWidth(450);&nbsp; &nbsp; &nbsp; &nbsp; table.setPrefHeight(300);&nbsp; &nbsp; &nbsp; &nbsp; table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);&nbsp; &nbsp; &nbsp; &nbsp; // Status message text&nbsp; &nbsp; &nbsp; &nbsp; actionStatus = new Text();&nbsp; &nbsp; &nbsp; &nbsp; actionStatus.setFill(Color.FIREBRICK);&nbsp; &nbsp; &nbsp; &nbsp; Button button = new Button("Print");&nbsp; &nbsp; &nbsp; &nbsp; // Vbox&nbsp; &nbsp; &nbsp; &nbsp; VBox vbox = new VBox(20);&nbsp; &nbsp; &nbsp; &nbsp; vbox.setPadding(new Insets(25, 25, 25, 25));;&nbsp; &nbsp; &nbsp; &nbsp; vbox.getChildren().addAll(hb, table, actionStatus, button);&nbsp; &nbsp; &nbsp; &nbsp; // Scene&nbsp; &nbsp; &nbsp; &nbsp; Scene scene = new Scene(vbox, 500, 475); // w x h&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(scene);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; &nbsp; &nbsp; // Select the first row&nbsp; &nbsp; &nbsp; &nbsp; table.getSelectionModel().select(0);&nbsp; &nbsp; &nbsp; &nbsp; Book book = table.getSelectionModel().getSelectedItem();&nbsp; &nbsp; &nbsp; &nbsp; actionStatus.setText(book.toString());&nbsp; &nbsp; &nbsp; &nbsp; List<List<Book>> bookLists = partition(data, 10);&nbsp; &nbsp; &nbsp; &nbsp; button.setOnAction((event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrinterJob printerJob = PrinterJob.createPrinterJob();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printerJob.showPrintDialog(primaryStage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bookLists.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.addAll(bookLists.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printerJob.printPage(table);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printerJob.endJob();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private ObservableList getInitialTableData()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List list = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Thief", "Fuminori Nakamura"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Of Human Bondage", "Somerset Maugham"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Bluest Eye", "Toni Morrison"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("I Am Ok You Are Ok", "Thomas Harris"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Magnificent Obsession", "Lloyd C Douglas"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("100 Years of Solitude", "Gabriel Garcia Marquez"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("What the Dog Saw", "Malcolm Gladwell"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Fakir", "Ruzbeh Bharucha"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Hobbit", "J.R.R. Tolkien"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Strange Life of Ivan Osokin", "P.D. Ouspensky"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Hunt for Red October", "Tom Clancy"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Coma", "Robin Cook"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Catskill Eagle", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Children of Men", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Clouds of Witness", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Confederacy of Dunces", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Consider Phlebas", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Consider the Lilies", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Cover Her Face", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Cricket on the Hearth", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Curious Incident of the Dog in the Night-Time", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Daffodil Sky", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Dance Dance Dance", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Darkling Plain", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Thief", "Fuminori Nakamura"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Of Human Bondage", "Somerset Maugham"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Bluest Eye", "Toni Morrison"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("I Am Ok You Are Ok", "Thomas Harris"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Magnificent Obsession", "Lloyd C Douglas"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("100 Years of Solitude", "Gabriel Garcia Marquez"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("What the Dog Saw", "Malcolm Gladwell"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Fakir", "Ruzbeh Bharucha"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Hobbit", "J.R.R. Tolkien"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Strange Life of Ivan Osokin", "P.D. Ouspensky"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Hunt for Red October", "Tom Clancy"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Coma", "Robin Cook"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Catskill Eagle", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Children of Men", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Clouds of Witness", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Confederacy of Dunces", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Consider Phlebas", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Consider the Lilies", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Cover Her Face", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Cricket on the Hearth", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Curious Incident of the Dog in the Night-Time", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("The Daffodil Sky", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("Dance Dance Dance", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Book("A Darkling Plain", "xxx"));&nbsp; &nbsp; &nbsp; &nbsp; return FXCollections.observableList(list);&nbsp; &nbsp; }&nbsp; &nbsp; private static <T> List<List<T>> partition(Collection<T> members, int maxSize)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<List<T>> res = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; List<T> internal = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (T member : members) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; internal.add(member);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (internal.size() == maxSize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.add(internal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; internal = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (internal.isEmpty() == false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.add(internal);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return res;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java