1

I have a simple Spring Boot + JavaFX 8 application like so:

package com;

import javafx.application.Application;
import javafx.print.PrinterJob;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Main extends Application {

    protected ConfigurableApplicationContext springContext;

    public static void main(final String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        springContext = springBootApplicationContext();
    }

    @Override
    public void start(final Stage stage) {
        // Create the PrinterJob
        PrinterJob job = PrinterJob.createPrinterJob();

        if (job == null)
        {
            return;
        }

        // Show the page setup dialog
        boolean proceed = job.showPageSetupDialog(stage);

        if (proceed)
        {
            System.out.println("SUCCESS");
        }
    }

    @Override
    public void stop() {
        springContext.close();
    }


    private ConfigurableApplicationContext springBootApplicationContext() {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class);
        String[] args = getParameters().getRaw().toArray(new String[0]);
        return builder.run(args);
    }
}

The problem is that it prints SUCCESS without showing a Page Setup at all regardless of the OS (I'm currently working on Linux). Example from here (section 5. Showing the Print Dialogs), however, works as intended. Why does my Spring Boot app not work while the second example does?

Colonder
  • 1,556
  • 3
  • 20
  • 40

1 Answers1

0

It turned out that Spring Boot sets the java.awt.headless property to true by default, which makes the app run in headless mode, hence not showing the print setup. I solved it simply debugging the code and then following those two SO questions:

so my final code is as follows:

package com;

import javafx.application.Application;
import javafx.print.PrinterJob;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Main extends Application {

    protected ConfigurableApplicationContext springContext;

    public static void main(final String[] args) {
        System.setProperty("java.awt.headless", "false");
        Application.launch(args);
    }

    @Override
    public void init() {
        springContext = springBootApplicationContext();
    }

    @Override
    public void start(final Stage stage) {
        // Create the PrinterJob
        PrinterJob job = PrinterJob.createPrinterJob();

        if (job == null)
        {
            return;
        }

        // Show the page setup dialog
        boolean proceed = job.showPageSetupDialog(stage);

        if (proceed)
        {
            System.out.println("SUCCESS");
        }
    }

    @Override
    public void stop() {
        springContext.close();
    }


    private ConfigurableApplicationContext springBootApplicationContext() {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class);
        String[] args = getParameters().getRaw().toArray(new String[0]);
        return builder.run(args);
    }
}
Colonder
  • 1,556
  • 3
  • 20
  • 40