简体   繁体   中英

How to disable parent node in javafx while printing?

I have created a program that decodes base64 strings and displays an image, using a lot of fxml/javafx. I'm implementing a Print Image button that takes the data from the imageView node and sends it to the printer.

I want clicking the Print Image button to disable all buttons during the printing process. So far I am unable to accomplish this, despite having tried a few things:

Here is an attempt to simply disable an individual button:

// upon Print Image button pressed
UploadButton.setDisable(true);
if (printingComplete) {
UploadButton.setDisable(false);
}

This does not work. I'm not getting any errors. It just doesn't disable the button in question.

Here is an attempt to grab the parent node and disable it so that all buttons are disabled:

// upon Print Image button pressed
Node node = imageDisplay;
    while (node != null) {
    node = node.getParent();
    }
    Node parentNode = node;
    parentNode.setDisable(true);

This also does not work. No errors, just no change in button functionality.

Where am I going wrong here?

Edit: Solution posted in comments

Try this code to disable ALL buttons in your container ( root in my case):

root.getChildren().forEach(node -> {
            if(node instanceof Button){
                node.setDisable(true);
            }
        });

Do the same to enable them, just replace

node.setDisable(true);

with

node.setDisable(false);

Note: I am implementing a similar feature in my app, your idea of "disabling functions" by "disabling buttons" may not be as effective, there might be other nodes in your stage that fire an event.
The way i am implementing the "disabling" feature:

  1. Create an empty container (not visible) above all others (AnchorPane in my case)
  2. Set % on its transparency (grey filter in my case) using css :

-fx-background-color: linear-gradient(to right, rgba(72, 85, 99, 0.69), rgba(41, 50, 60, 0.69));

  1. When you want to disable all other events, you make this AnchorPane visible (covering all nodes) and giving a visual effect to users.

  2. After you are finished, you disable its visibility.

This way you will cover X button in window if you are using an undecorated stage, so users can't close the app till process is finished (unless they press ALT+F4 in keyboard).
Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM