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
You can use a ChoiceDialog for that purpose to choose a Printer
from the Set
of printers returned by Printer.getAllPrinters:
ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters());
dialog.setHeaderText("Choose the printer!");
dialog.setContentText("Choose a printer from available printers");
dialog.setTitle("Printer Choice");
Optional<Printer> opt = dialog.showAndWait();
if (opt.isPresent()) {
Printer printer = opt.get();
// start printing ...
}
Of course you could use any other way to choose a single item from a list of items too, if you prefer not to use a dialog. E.g.
ListView
ComboBox
TableView
BTW: the size of nodes will be 0, unless they were layouted, which could cause
double scaleX = node.getBoundsInParent().getWidth();
double scaleY = node.getBoundsInParent().getHeight();
node.getTransforms().add(new Scale(scaleX, scaleY));
to scale it to 0
. For nodes not already displayed, you need to layout them yourself (see this answer: https://stackoverflow.com/a/26152904/2991525):
Group g = new Group(node);
Scene scene = new Scene(g);
g.applyCss();
g.layout();
double scaleX = node.getBoundsInParent().getWidth();
double scaleY = node.getBoundsInParent().getHeight();
But I'm not sure what you're trying to achieve with the scaling anyway... The larger the node, the greater the scaling factor is not really a reasonable thing to do, especially if the height and width differ.