Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainGui extends Application {
- @Override
- public void start(Stage primaryStage) throws Exception {
- Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
- primaryStage.setTitle("Login");
- primaryStage.setScene(new Scene(root));
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- **LoginController for login button**
- public class LoginController {
- BackendInterface backendInterface;
- @FXML
- TextField username;
- @FXML
- PasswordField password;
- @FXML
- Button loginButton;
- @FXML
- Label loginLabel;
- @FXML
- public void loginButtonPress(){
- if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {
- loginLabel.setText("Please enter data in the fields below");
- } else {
- //initialises backend interface with username and password
- backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());
- //opens a connection to the database
- backendInterface.openConnection();
- if (backendInterface.getConnectionResponse() == "success"){
- //return and print response
- System.out.println(backendInterface.getConnectionResponse());
- //directs the user to the dashboard after successful login
- try{
- FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
- Parent root1 = (Parent) fxmlLoader.load();
- Stage stage = new Stage();
- stage.initModality(Modality.APPLICATION_MODAL);
- stage.setTitle("Welcome " + username.getText());
- stage.setScene(new Scene(root1));
- stage.show();
- //Closes the login screen window
- Stage stage2 = (Stage) loginButton.getScene().getWindow();
- stage2.hide();
- } catch (Exception e){
- e.printStackTrace();
- }
- } else {
- //warns user of invalid login
- loginLabel.setText(backendInterface.getConnectionResponse());
- }
- }
- }
- }
- **DashboardController for button on dashboard**
- public class DashboardController {
- @FXML
- public void doStuff(){
- LoginController loginController = new LoginController();
- loginController.backendInterface.printSomething();
- }
- }
- FXMLLoader loader = new FXMLLoader();
- loader.setLocation(getClass().getResource("dashboard.fxml"));
- loader.load();
- Parent p = loader.getRoot();
- Stage stage = new Stage();
- stage.setScene(new Scene(p));
- DashboardController dashboardController = loader.getController();
- dashboardController.setBackendInterface(backendInterface);
- **Second Controller retrieve object method**
- public void setBackendInterface(BackendInterface backendInterface) {
- this.backendInterface = backendInterface;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement