2017-02-07 97 views
2

我在LoginController中獲取管理對象。它不是空的沒有問題。當我設置這個對象AdminController它不工作。在我初始化顯示「null」的syso結果。我需要在我的其他控制器中使用這個對象。使用swing我是在控制器構造函數和swing構造器中完成的。但我不知道如何用JavaFx做到這一點。對不起,我的英語不好。但如果你看代碼,你會理解我。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); 

    } 

} 

回答

1

加載FXML文件時初始化方法被調用,你應該建立在AdminController另一個init方法,將做到這一點,你需要與管理對象的所有初始化,並調用之後,調用此方法二傳手:

AdminController adminController = loader.getController(); 
adminController.setAdmin(admin); 
adminController.initController(); 
+0

非常感謝你Zoltanik。我在管理控制器中創建加載方法。我設置管理然後調用加載。現在我可以在組件場景中首次顯示用戶信息。 –

相關問題