Printing TableView Contents in JavaFX

♀尐吖头ヾ 提交于 2021-02-10 11:47:50

问题


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:

enter image description here




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!