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.
Probably a current limitation of JavaFX as described by JDK-8088395.
So you have these options:
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With