在JavaFX中打印TableView内容 [英] Printing TableView Contents in JavaFX
问题描述
我正在学习JavaFX.我创建了我的TableView
并用数据填充了它.我添加了一个按钮,以便在单击该按钮时可以打印表格内容.
I am learning JavaFX. I created my TableView
and populated it with data. I added a Button such that when it is clicked, the table contents can be printed.
这是完整的代码:
`public final class TableViewSample2 extends Application {
private TableView<Person> table;
private ObservableList<Person> list;
public void createTable() {
table = new TableView<>();
table.setEditable(true);
list = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com"));
//associating data with the table columns
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("name"));
TableColumn SurnameCol = new TableColumn("Surname");
SurnameCol.setMinWidth(100);
SurnameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("surname"));
TableColumn emailCol = new TableColumn("Emil");
emailCol.setMinWidth(100);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(list);
table.getColumns().addAll(firstNameCol, SurnameCol, emailCol);
}
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
this.createTable();
Label label = new Label("My Address Book");
Button button = new Button("Print");
//printing
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println(" can I print?");
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob.showPrintDialog(primaryStage) && printerJob.printPage(table))
{
printerJob.endJob();
System.out.println("printed");
}
}
});
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, button);
root.getChildren().add(vbox);
primaryStage.setTitle("Sample TableView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}}
当我运行程序时,该表将按预期显示.完美的.但是,当我单击打印"按钮时,没有任何反应.看到此代码后,我正在寻找如何打印TableView内容的方法.我试图在控制台中输出一些消息,但是什么也没显示.
When I run the program, the table is shown as it is supposed to. Perfect. However, when I click the print Button nothing is happening. I was looking on how to print TableView contents when I saw this code. I tried to output some message in console but nothing is showing.
我该如何解决?
推荐答案
我复制了您的代码(以及oracle站点中的类Person
),并且效果很好.正如ItachiUchiha所说,我还假定.createPrinterJob()不返回对象.也许您尚未安装任何打印机.
NetBeans显示了我的旧HP打印机的一些详细信息:
I copied your code (and the class Person
from the oracle site :) and it works perfect. As ItachiUchiha commented I also assume that .createPrinterJob() does not return an object. Maybe you haven't installed any printer.
NetBeans shows some details of my old HP printer:
这篇关于在JavaFX中打印TableView内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!