am working on an application , this application is in javafx, in this application we are taking food orders and this order we have to print using different printer, some printe
I know this is 4 years old, but for any future people coming through to find ways to do this like i once was, there is a much simpler method of allowing use choice. All that needs to be used is the method .showPrintDialog(Stage stage) (I tried to post an image on the site linked at the bottom but don't have enough reputation since im new)
I have put some code below for reference to how you could use this:
import javafx.print.PrinterJob;
import javafx.collections.ObservableSet;
import javafx.print.Printer;
import javafx.scene.Node;
import javafx.stage.Stage;
public class ExampleClass
{
public static void printPageSetup(Node node, Stage owner){
PrinterJob job = PrinterJob.createPrinterJob();
boolean proceed = job.showPrintDialog(owner);
boolean goforward = job.showPageSetupDialog(owner);
if (job == null){
return;
}
if (proceed && goforward){
print2(job, node);
}
}
public static void print2(PrinterJob job, Node node){
if (job != null){
boolean printed = job.printPage(node);
if (printed){
job.endJob();
}
}
else{
System.out.println("Printing failed");
}
}
}
An really bad example use of this would be this a method like this:
// testPrint would just be attached to a random button of your own choice
public void testPrint(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("AN_EXAMPLE_FXML_PAGE.fxml"));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
ExampleClass.printPageSetup(root, stage);
}
For further information go to javacodeforgeeks' article on this