Reputation: 41
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 printer will be in the kitchen some in the head office. In my system i need list of printers and when i press print button from my application that time i will select printer from the list. So the print job will passed to the selected printer.How i will done this in my javafx application?
Am using this following method but it pass printjob to default printer which is selected by the system not by the applicaiton:-
public void print(Node node) {
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
double scaleX = node.getBoundsInParent().getWidth();
double scaleY = node.getBoundsInParent().getHeight();
node.getTransforms().add(new Scale(scaleX, scaleY));
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
}
}
This is how am passing printer to print job, but not getting print from the printer:
ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters());
//ChoiceDialog dialog = new ChoiceDialog(printerName1, printerName2, printerName3, printerName4, printerName5);
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();
PrinterJob job = PrinterJob.createPrinterJob();
job.setPrinter(printer);
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
}
}
Upvotes: 0
Views: 3926
Reputation: 11
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
Upvotes: 1
Reputation: 82491
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.
Upvotes: 1