Toymos
Toymos

Reputation: 23

Setting CSS to use when printing from JavaFX

I am writing a program which needs to print notes about deliveries made. Currently using the JavaFX 8 printing methods I've been able to create a basic delivery note however the default style for displaying a TableView contains a lot of greyscale which makes a physical printed copy look strange and difficult to read.

I have used CSS to style the TableView to be solid black and white however when printing it seems to ignore the CSS that I have set.

Here's what I have currently:

private void printDeliveryNote(){
    PrinterJob job = PrinterJob.createPrinterJob();
    PageLayout pageLayout = Printer.getDefaultPrinter().createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
    Group pane = new Group();
    pane.getChildren().addAll(getNodeToPrint());
    pane.getStylesheets().add("css/main.css");

    if (job != null && job.showPrintDialog(new Stage())){
        boolean success = job.printPage(pageLayout, pane);
        if (success){
            job.endJob();
        }
    }
}

private Node getNodeToPrint() {
    Group group = new Group();
    Label prntDeliveryId = new Label("Delivery Id: " + txtDeliveryId.getText());
    prntDeliveryId.setLayoutX(txtDeliveryId.getLayoutX());
    prntDeliveryId.setLayoutY(txtDeliveryId.getLayoutY());

    Label prntDate = new Label("Date: " + txtDate.getText());
    prntDate.setLayoutX(txtDate.getLayoutX() - 20);
    prntDate.setLayoutY(txtDate.getLayoutY());

    Label prntTitle = new Label(lblTitle.getText());
    prntTitle.setLayoutX(lblTitle.getLayoutX());
    prntTitle.setLayoutY(lblTitle.getLayoutY());
    prntTitle.setFont(lblTitle.getFont());

    ImageView imgBarcode = new ImageView(SwingFXUtils.toFXImage(handleBarcode(txtDeliveryId.getText()), null));
    imgBarcode.setLayoutX(txtDeliveryId.getLayoutX());
    imgBarcode.setLayoutY(txtDeliveryId.getLayoutY());

    TableView<InstrumentContainer> prntInstrumentList = new TableView<InstrumentContainer>();
    setupInstrumentList(prntInstrumentList);
    prntInstrumentList.setLayoutY(lstInstruments.getLayoutY() + 40);
    prntInstrumentList.setPrefWidth(lstInstruments.getPrefWidth());
    System.out.println(prntInstrumentList.getHeight());
    prntInstrumentList.setLayoutX(lstInstruments.getLayoutX());
    group.getChildren().addAll(
            prntDeliveryId,
            prntDate,
            prntInstrumentList,
            prntTitle,
            imgBarcode
    );
    return group;
}

The CSS file looks like this (which works when displaying on a form rather than when printed):

.table-row-cell{
-fx-table-cell-border-color: #424242;
}
.column-header{
-fx-border-color: #424242;
-fx-background-color: #ffffff;
}

The TableView with the style applied to it on the form looks like this:

How it should look

But when printed it still has the default style: How it actually looks

Any ideas of how to style a node which is being printed?

Upvotes: 2

Views: 572

Answers (1)

Michael
Michael

Reputation: 44210

This is probably a precedence issue. JavaFX includes its own default stylesheet modena.css* which can, and often does, use higher specificity rules than any custom ones you may have added.

One way to get around this would be to add !important to each of the declarations:

.table-row-cell {
    -fx-table-cell-border-color: #424242 !important;
}
.column-header {
    -fx-border-color: #424242 !important;
    -fx-background-color: #ffffff !important;
}

If you have a lot of properties to override this might become a bit tedious. You could look at the default styles in the default stylesheet (linked above) and use a specificity calculator to work out how to make your selector more specific than the default one.


* Older versions may use different default stylesheets.

Upvotes: 1

Related Questions