I want to disable multiple buttons of the same "type" in JavaFX. For example, I have multiple cancel button for different operations, but I'd like to enable / disable them all at the same time, but it seems I can't access them inside a LinkedList.
In the below "minimal and reproducible" example, when pressing start, it should enable the (disabled by default in fxml) cancel buttons, but it does not.
DemoController.java
public class DemoController {
@FXML
Button startBtn;
@FXML
Button cancelBtn1; // disabled by default
@FXML
Button cancelBtn2; // disabled by default
@FXML
Tab tab1;
public SDK sdk;
List<Node> cancelBtnGroup = new LinkedList<Node>();
public void initialize() {
cancelBtnGroup.add(cancelBtn1);
cancelBtnGroup.add(cancelBtn2);
startBtn.setOnAction(event -> {
tab1.setDisable(true);
startBtn.setDisable(true);
System.out.println("Disabling cancel buttons");
HashMap<String, String> test = new HashMap<String, String>();
test.put("disableCancel", "false");
sdk.onEvent(test);
});
initializeSDK();
}
public void initializeSDK() {
sdk = new SDK() {
@Override
public void onEvent(HashMap<String, String> e) {
if(e.containsKey("disableCancel")) {
for(Node btn : cancelBtnGroup) {
btn.setDisable(Boolean.parseBoolean(e.get("disableCancel")));
}
}
}
};
}
}
It seems that I'm overwriting the buttons somehow, but I don't see how. I feel like I'm not understanding the scope inside some of the functions. I tried to make the code as simplified and readable as possible, so if I'm missing something extremely obvious that may be why. The SDK object does not contain objects with the same name, to be sure that wasn't the issue I've changed the names.
The issue was that I'm disabling the tab which disables all the buttons in it. I didn't notice it because I only have 2 buttons, one of which gets disabled anyway, but the other should not. So to me it seemed that the button was just not responding.
Solution
Remove
tab1.setDisable(true)
I was disabling all of my tabs so mid-operation the user could not switch tabs, but I forgot that if you disable the current tab it also disables buttons.
I want to thank @Slaw for the help and putting up with my attitude. (I've had annoying experiences with SO too many times before, so I have a predisposition about answers that post the same annoying link 1000 times. I know I need a minimal reproducible example, but that's not always easy to do especially when you're working with an external SDK and already extensive code. I was trying to make it minimal and reproducible.