Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public class MainGui extends Application {
  2.  
  3. @Override
  4. public void start(Stage primaryStage) throws Exception {
  5.  
  6. Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
  7. primaryStage.setTitle("Login");
  8. primaryStage.setScene(new Scene(root));
  9. primaryStage.show();
  10.  
  11. }
  12.  
  13.  
  14. public static void main(String[] args) {
  15. launch(args);
  16. }
  17. }
  18.  
  19.  
  20. **LoginController for login button**
  21.  
  22. public class LoginController {
  23.  
  24. BackendInterface backendInterface;
  25.  
  26. @FXML
  27. TextField username;
  28.  
  29. @FXML
  30. PasswordField password;
  31.  
  32. @FXML
  33. Button loginButton;
  34.  
  35. @FXML
  36. Label loginLabel;
  37.  
  38. @FXML
  39. public void loginButtonPress(){
  40.  
  41. if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {
  42.  
  43. loginLabel.setText("Please enter data in the fields below");
  44.  
  45. } else {
  46.  
  47. //initialises backend interface with username and password
  48. backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());
  49.  
  50. //opens a connection to the database
  51. backendInterface.openConnection();
  52.  
  53. if (backendInterface.getConnectionResponse() == "success"){
  54.  
  55. //return and print response
  56. System.out.println(backendInterface.getConnectionResponse());
  57.  
  58. //directs the user to the dashboard after successful login
  59. try{
  60.  
  61. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
  62. Parent root1 = (Parent) fxmlLoader.load();
  63. Stage stage = new Stage();
  64. stage.initModality(Modality.APPLICATION_MODAL);
  65. stage.setTitle("Welcome " + username.getText());
  66. stage.setScene(new Scene(root1));
  67. stage.show();
  68.  
  69. //Closes the login screen window
  70. Stage stage2 = (Stage) loginButton.getScene().getWindow();
  71. stage2.hide();
  72.  
  73. } catch (Exception e){
  74. e.printStackTrace();
  75. }
  76.  
  77. } else {
  78. //warns user of invalid login
  79. loginLabel.setText(backendInterface.getConnectionResponse());
  80. }
  81. }
  82. }
  83. }
  84.  
  85.  
  86. **DashboardController for button on dashboard**
  87.  
  88. public class DashboardController {
  89.  
  90. @FXML
  91. public void doStuff(){
  92.  
  93. LoginController loginController = new LoginController();
  94.  
  95. loginController.backendInterface.printSomething();
  96.  
  97.  
  98. }
  99.  
  100. }
  101.  
  102. FXMLLoader loader = new FXMLLoader();
  103. loader.setLocation(getClass().getResource("dashboard.fxml"));
  104. loader.load();
  105. Parent p = loader.getRoot();
  106. Stage stage = new Stage();
  107. stage.setScene(new Scene(p));
  108.  
  109. DashboardController dashboardController = loader.getController();
  110. dashboardController.setBackendInterface(backendInterface);
  111.  
  112. **Second Controller retrieve object method**
  113.  
  114. public void setBackendInterface(BackendInterface backendInterface) {
  115. this.backendInterface = backendInterface;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement