espirio
espirio

Reputation: 189

Print dialog does not come to the fore

By PrinterJob of JavaFx can call the Print Dialog. My problem is that the dialog when calling does not come to the fore.

Here is my example:

import javafx.application.Application;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Printexample extends Application
{

  @Override
  public void start( final Stage primaryStage )
  {

    final PrinterJob job = PrinterJob.createPrinterJob( Printer.getDefaultPrinter() );
    final Button b = new Button( "Print Dialog" );
    b.setOnAction( event -> job.showPrintDialog( primaryStage ) );
    final BorderPane pane = new BorderPane( b );
    primaryStage.setMinWidth( 400 );
    primaryStage.setMinHeight( 300 );
    primaryStage.setTitle( "Print" );
    final Scene scene = new Scene( pane );
    primaryStage.setScene( scene );


    primaryStage.centerOnScreen();
    primaryStage.addEventFilter( KeyEvent.KEY_PRESSED, event ->
    {
      if ( event.getCode().equals( KeyCode.ESCAPE ) )
      {
        primaryStage.close();
      }
    } );
    primaryStage.show();

  }

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

The second problem: The frame is not modal, therefore it can lead to errors.

Information: I use Java 8_92.

Upvotes: 5

Views: 1960

Answers (3)

harryssuperman
harryssuperman

Reputation: 511

These Problem happen to me today with a spring boot application and the same Code under Windows 10.

I write here the solution, that for me works, because it was the same issue.

Spring is setting by default the java.awt.headless Property with true.

In the Implementation of showPrintDialog Method there is a bug checking about headless returning true but not showing the dialog.

The solution for me was setting the headless property in Spring as false as described Run Spring Boot Headless by Efe Kahraman

Upvotes: 0

AndreLDM
AndreLDM

Reputation: 2198

Probably a current limitation of JavaFX as described by JDK-8088395.

So you have these options:

  1. Wait this to eventually be fixed in an update or JavaFX 9.
  2. Write yourself a custom dialog and then communicate with the print APIs to populate it, as suggested in JDK-8098009.
  3. Block your scene using a overlay, show the print dialog and then remove the overlay. You'll also need to prevent the window closing while the scene is blocked.
  4. Use AWT Print Dialog (kludge, you've been warned), e.g.:

 

java.awt.print.PrinterJob printJob = PrinterJob.getPrinterJob();
Button b = new Button("Print Dialog");
b.setOnAction(event -> {
    JFrame f = new JFrame();
    printJob.printDialog();
    // Stage will be blocked(non responsive) until the printDialog returns
});

Upvotes: 5

joemas91
joemas91

Reputation: 14

I think you might be missing a peice of code to send the stage to the front.

Try adding: stage.toFront();

Source: http://www.java2s.com/Code/Java/JavaFX/Movingstagewindowtofront.htm

Upvotes: -1

Related Questions