Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print dialog does not come to the fore

Tags:

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.

like image 996
espirio Avatar asked Apr 25 '16 09:04

espirio


1 Answers

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
});
like image 106
AndreLDM Avatar answered Sep 28 '22 01:09

AndreLDM