I am working on a netbeansIDE javaFXML project and when i login with username and password to a second screen i want the second screens button be disabled. I have tried to disable the button in the other controller like this by using Getter but nothing happens when i am logged in. I really hope something can help, i tried to search help and i found some, and tried but ended with this and now i am stuck.
Here is my code:
{@FXML
private void handleLogin(ActionEvent event) throws IOException {
String username = txtUsername.getText();
String password = txtPassword.getText();
Button pressed_button = (Button) event.getSource();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Screen3_Scada.fxml"));
loader.load();
if(pressed_button==btnscadalogin){
if (loginLoad.validateSCADA(username, password)) {
try {
AnchorPane pane = FXMLLoader.load(getClass().getResource("Screen3_Scada.fxml"));
RootPane.getChildren().setAll(pane);
Screen3_Scada_Controller getbtnPLC = loader.getController();
getbtnPLC.getBtngotoPLC().setDisable(true);
}catch (IOException ex) {
System.err.println("Error in method GotoScreen2 class FXML_Screen1_loginController");
}
}else{
System.err.println("Error, wrong username or password");
}
}
}}
You're loading the scene twice:
loader.load();
...
AnchorPane pane = FXMLLoader.load(getClass().getResource("Screen3_Scada.fxml"));
getbtnPLC
is not the controller instance that is created when you load the scene you add to the existing one.
You should use the loaded result that is the result of the loader.load();
call:
if (pressed_button == btnscadalogin) {
if (loginLoad.validateSCADA(username, password)) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Screen3_Scada.fxml"));
try {
AnchorPane pane = loader.load();
RootPane.getChildren().setAll(pane);
Screen3_Scada_Controller getbtnPLC = loader.getController();
getbtnPLC.getBtngotoPLC().setDisable(true);
} catch (IOException ex) {
System.err.println("Error in method GotoScreen2 class FXML_Screen1_loginController");
}
}
I do recommend using different methods for handling ActionEvent
s from different sources. This is usually the better alternative than checking which one of multiple nodes is the source of the event.