How pass a print job to a specific printer in javafx applcation?

前端 未结 2 681
甜味超标
甜味超标 2021-01-26 03:36

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

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-26 03:56

    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 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.

提交回复
热议问题