Running the following program on mac leads to the main application window to hang on my mac system, when you attempt to print.
public class PrintSomeText extends Application {
private Stage stage;
private TextFlow flow;
@Override public void start(Stage stage) throws Exception {
this.stage = stage;
Font normalFont = Font.font("Helvetica", 96);
Font boldFont = Font.font("Helvetica", FontWeight.BOLD, 170);
Font italicFont = Font.font("Helvetica", FontPosture.ITALIC, 96);
Text hello = new Text("Hello ");
hello.setFont(normalFont);
Text bold = new Text("Bold ");
bold.setFont(boldFont);
bold.setFill(Color.ORANGE);
bold.setStroke(Color.BLACK);
bold.setStrokeWidth(5);
Text world = new Text("World");
world.setFont(italicFont);
world.setFill(Color.RED);
Menu file = new Menu("File");
MenuItem print = new MenuItem("Print");
print.setOnAction(this::print);
file.getItems().add(print);
MenuBar bar = new MenuBar();
bar.setUseSystemMenuBar(true);
bar.getMenus().add(file);
flow = new TextFlow(hello, bold, world);
flow.setTextAlignment(TextAlignment.CENTER);
flow.setMaxSize(TextFlow.USE_PREF_SIZE, TextFlow.USE_PREF_SIZE);
Scene s = new Scene(new StackPane(flow, bar), 800, 400);
stage.setScene(s);
stage.show();
}
private void print(Event e) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(stage)) {
if (job.printPage(flow)) {
job.endJob();
}
}
}
public static void main(String[] args) {
launch(args);
}
}
public class PrintSomeText extends Application {
private Stage stage;
private TextFlow flow;
@Override public void start(Stage stage) throws Exception {
this.stage = stage;
Font normalFont = Font.font("Helvetica", 96);
Font boldFont = Font.font("Helvetica", FontWeight.BOLD, 170);
Font italicFont = Font.font("Helvetica", FontPosture.ITALIC, 96);
Text hello = new Text("Hello ");
hello.setFont(normalFont);
Text bold = new Text("Bold ");
bold.setFont(boldFont);
bold.setFill(Color.ORANGE);
bold.setStroke(Color.BLACK);
bold.setStrokeWidth(5);
Text world = new Text("World");
world.setFont(italicFont);
world.setFill(Color.RED);
Menu file = new Menu("File");
MenuItem print = new MenuItem("Print");
print.setOnAction(this::print);
file.getItems().add(print);
MenuBar bar = new MenuBar();
bar.setUseSystemMenuBar(true);
bar.getMenus().add(file);
flow = new TextFlow(hello, bold, world);
flow.setTextAlignment(TextAlignment.CENTER);
flow.setMaxSize(TextFlow.USE_PREF_SIZE, TextFlow.USE_PREF_SIZE);
Scene s = new Scene(new StackPane(flow, bar), 800, 400);
stage.setScene(s);
stage.show();
}
private void print(Event e) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(stage)) {
if (job.printPage(flow)) {
job.endJob();
}
}
}
public static void main(String[] args) {
launch(args);
}
}
- duplicates
-
JDK-8095848 Printing: PrinterJob.createPrinterJob() hangs on mac
-
- Closed
-