作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:我想保存一个包含 n 个部分的 TableView 数据的文档。该函数应每 10 个元素拆分表,并将每 10 个元素保存在干净的表中。我已经找到了一个分页示例,并尝试使用模 % 运算符,但它的行为对我来说很奇怪。
私有(private)无效 printDoc(){
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 文件的输出如下所示:
the output image showing a TableView
两次表中都有全部 20 个元素。但我希望将其拆分为单独的表格文件中的每 10 个元素,以便在不止一页上打印收据,并具有准确的页眉、页脚,并且在文档的中间我想打印具有静态/固定尺寸的表格。
最佳答案
您可以尝试类似于下面的代码。该代码打印包含十个项目的 TableView
。然后它会清除 TableView
并添加另外 10 个要打印的项目。它将继续此过程,直到打印所有项目。
它打开打印对话框。这将允许您将打印保存为 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;
}
}
关于java - 如何将 TableView (JavaFX 11) 分成 n 个部分以另存为 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54987351/
我有 json 数据: { "products": [ { "productId" : 0, "productImg" : "../img/product-ph
我是一名优秀的程序员,十分优秀!