我在LoginController中使用了Admin对象。它不是空的没有问题。当我设置此对象AdminController它不工作。在我的初始化syso结果中显示" null"。我需要在我的其他控制器中使用此对象。使用swing我在控制器构造器和摇摆构造器中这样做。但我不知道如何用JavaFx做到这一点。对不起,我的英语不好。但如果你看代码,你会理解我。
public class UserLoginController extends AbstractController {
@FXML
private Button joinBtn;
@FXML
private Button exitBtn;
@FXML
private Label statusLabel;
@FXML
private TextField userNo;
@FXML
private PasswordField password;
@FXML
void buttonEvent(ActionEvent event) {
if (joinBtn == event.getSource()) {
String userType = userNo.getText().substring(0, 2);
switch (userType) {
case "10":
Admin admin = DBContext.getInstance().admins().isUserExist(Integer.parseInt(userNo.getText()), password.getText());
if(admin != null) {
final String fxml = "/educationManagement/view/AdminPanel.fxml";
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
Parent root = (Parent) loader.load();
AdminController adminController = loader.getController();
adminController.setAdmin(admin);
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
stage.setTitle(admin.getFirstName() + " " + admin.getLastName());
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
System.out.println("fuck");
}
} else {
statusLabel.setText("Kullanıcı Adı veya Şifre Yanlış");
userNo.setText("");
password.setText("");
}
break;
case "40":
break;
case "50":
break;
default:
statusLabel.setText("Kullanıcı Adı Bulunamadı");
break;
}
}
}
}
public class AdminController implements Initializable{
private Admin admin;
@FXML
private TableView<Student> studentTable;
@FXML
private Label firstNameLbl;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println(admin);
}
}
答案 0 :(得分:2)
在加载FXML文件时正在调用initialize方法,你应该在AdminController中创建另一个init方法,它将执行admin对象所需的所有初始化,并在调用setter后调用此方法:
AdminController adminController = loader.getController();
adminController.setAdmin(admin);
adminController.initController();