I tried to make multi user login form but, I have a problem with passing value from one controller to another and I dont really know what is going on.
First controller
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();
}
}
}
And second controller where i want to pass 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);
}
}
And this exception appears after trying to log in
javafx.fxml.LoadException:
unknown path
I found out that
imie.setText(value);
cause this exception. Without it, everything is fine.
The variable value is still null when called in the initialize Method of Employer Controller. In your example, you could just change your getUser Method like this:
public void getUser(String user){
setValue(user);
imie.setText(value);
}
Also, a method that is actually setting a value should not be called "get..." and it's most likely not a good idea to declare the Label static.