问题
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.
Here is the whole code for :
`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);
}}
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.
How can I resolve this?
回答1:
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:
回答2:
Since you are already using the primaryStage, its owner will be null
Try replacing primaeyStage.getOwner()
with primaryStage
button.setOnAction((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");
}
});
来源:https://stackoverflow.com/questions/27998443/printing-tableview-contents-in-javafx