我创建了一个程序,使用大量的fxml / javafx解码base64字符串并显示图像。我正在实现一个Print Image按钮,该按钮从imageView节点获取数据并将其发送到打印机。
我想在打印过程中单击“打印图像”按钮以禁用所有按钮。到目前为止,尽管尝试过以下几点,但我无法做到这一点:
这是尝试简单地禁用单个按钮:
// upon Print Image button pressed
UploadButton.setDisable(true);
if (printingComplete) {
UploadButton.setDisable(false);
}
这不起作用。我没有收到任何错误。它只是不会禁用有问题的按钮。
这是尝试获取父节点并禁用它以便禁用所有按钮:
// upon Print Image button pressed
Node node = imageDisplay;
while (node != null) {
node = node.getParent();
}
Node parentNode = node;
parentNode.setDisable(true);
这也行不通。没有错误,按钮功能没有变化。
我在哪里错了?
编辑:在评论中发布的解决方案
尝试使用此代码禁用容器中的所有按钮(在我的情况下为root):
root.getChildren().forEach(node -> {
if(node instanceof Button){
node.setDisable(true);
}
});
做同样的事情来启用它们,只需更换
node.setDisable(true);
同
node.setDisable(false);
注意:我在我的应用程序中实现了类似的功能,通过“禁用按钮”实现“禁用功能”的想法可能不那么有效,您的舞台中可能还有其他节点可以触发事件。 我实现“禁用”功能的方式:
-fx-background-color:线性渐变(右边,rgba(72,85,99,0.69),rgba(41,50,60,0.69));
这样,如果您使用未修饰的舞台,您将在窗口中覆盖X按钮,因此用户无法关闭应用程序直到处理完成(除非他们按下键盘上的ALT + F4)。 希望能帮助到你!