CN1002
CN1002

Reputation: 1115

Printing TableView Contents in JavaFX

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", "[email protected]"),
            new Person("Isabella", "Johnson", "[email protected]"),
            new Person("Ethan", "Williams", "[email protected]"),
            new Person("Emma", "Jones", "[email protected]"),
            new Person("Michael", "Brown", "[email protected]"));

//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?

Upvotes: 2

Views: 5704

Answers (2)

chris
chris

Reputation: 1785

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

Upvotes: 1

ItachiUchiha
ItachiUchiha

Reputation: 36722

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");
     }
});

Upvotes: 0

Related Questions