The print dialog does not come to the fore

The JavaFxJob printer can call up 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, so it can lead to errors.

Info: I am using Java 8_92.

+5
source share
2 answers

Probably the current JavaFX limitation, as described by JDK-8088395 .

So, you have the following options:

  • Wait for this to be fixed in the update or in JavaFX 9.
  • Write yourself a custom dialog, and then talk to the printing APIs as suggested in JDK-8098009 .
  • Lock your scene with an overlay, show the print dialog, and then remove the overlay. You also need to prevent the window from closing while locking the scene.
  • Use AWT Print Dialog (kludge, you were warned), for example:

 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 }); 
+5
source

I think that you may miss a piece of code to send the scene to the fore.

Try adding: stage.toFront();

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

-1
source

Source: https://habr.com/ru/post/1247853/


All Articles