[英]JavaFX LoadException when passing value between controllers
我試圖創建多用戶登錄表單但是,我有一個問題,將值從一個控制器傳遞到另一個控制器,我真的不知道發生了什么。
第一控制器
public class Controller implements Initializable{
public Pracownik pracownik = new Pracownik();
@FXML
private Label isConnected;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPass;
private Connection conn;
// private ObservableList<Pracownik> lista = FXCollections.observableArrayList();
public void initialize(URL url, ResourceBundle rb){
conn = DBConnection.getConnection();
// lista = new Pracownik().getAll(conn);
}
public void login(ActionEvent event){
try {
if(pracownik.isLogin(conn, txtUsername.getText(), txtPass.getText()) ){
isConnected.setText("Correct");
if(pracownik.stanowisko(conn, txtUsername.getText(), txtPass.getText()) == 1){
Stage primarystage = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("/sample/BossView.fxml").openStream());
//BossController controller = (BossController) loader.getController();
Scene scene = new Scene(root);
primarystage.setScene(scene);
primarystage.show();
}
else {
Stage primarystage = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("/sample/EmployerView.fxml").openStream());
EmployerController controller = loader.getController();
controller.getUser(txtUsername.getText());
Scene scene = new Scene(root);
primarystage.setScene(scene);
primarystage.show();
}
}
else{
isConnected.setText("Błędne dane");
}
} catch (SQLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
和第二個控制器,我想傳遞txtUserName
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class EmployerController implements Initializable {
String value;
public void setValue(String value) {
this.value = value;
}
@FXML
private static Label imie;
@FXML
private Label nazwisko;
@FXML
private Label mail;
@FXML
private Label numer;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
imie.setText(value);
}
public void getUser(String user){
setValue(user);
}
}
嘗試登錄后會出現此異常
javafx.fxml.LoadException:
unknown path
我發現
imie.setText(value);
引起這個例外。 沒有它,一切都很好。
在初始化雇主控制器方法中調用時,變量值仍為null。 在您的示例中,您可以像這樣更改getUser方法:
public void getUser(String user){
setValue(user);
imie.setText(value);
}
另外,實際設置值的方法不應該被稱為“get ...”,並且將Label標准為靜態最可能不是一個好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.