我试图制作多用户登录表单,但是,我无法将值从一个控制器传递给另一个控制器,而且我真的不知道发生了什么。
第一个控制器
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);
导致此异常。没有它,一切都会很好。
答案 0 :(得分:0)
在Employer Controller的initialize方法中调用时,变量值仍为null。在您的示例中,您可以像这样更改getUser方法:
public void getUser(String user){
setValue(user);
imie.setText(value);
}
此外,实际上设置值的方法不应称为“ get ...”,并且最好将Label声明为静态。