1

So I do not get any errors when I execute my code. I also have setup controller in my fxml and code that I post below is of controller and main class.Since I already posted fxml in previous post.

package realEstateApplication;

import javafx.application.Application;
//import javafx.fxml.FXMLLoader;
//import javafx.scene.Parent;
//import javafx.scene.Scene;
import javafx.stage.Stage;
import realEstateApplication.controllers.loginViewController;

public class Main extends Application {

public Main() {
    System.out.println("Main invoked ... \n");
}

@Override
public void start(Stage primaryStage) throws Exception{
   // Parent loginRoot = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));
    loginViewController loginMVC =  new loginViewController();

   // Parent login = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));

   /* Parent admin = FXMLLoader.load(getClass().getResource("views/adminView.fxml"));
    Parent registerAdmin = FXMLLoader.load(getClass().getResource("views/registerAdminView.fxml"));
    Parent registerCustomer = FXMLLoader.load(getClass().getResource("views/registerCustomerView.fxml"));
    Parent registerCompany = FXMLLoader.load(getClass().getResource("views/registerCompanyView.fxml"));
    Parent registerFlat    = FXMLLoader.load(getClass().getResource("views/registerFlatView.fxml"));*/

   // Stage firstStage = new Stage();
   // firstStage.setTitle("Login");
   // firstStage.setScene(new Scene(loginRoot, 600, 400));
    //firstStage.show();

    /*Stage secondaryStage = new Stage();
    secondaryStage.setTitle("Administrator");
    secondaryStage.setScene(new Scene(admin, 600, 399));
    secondaryStage.show();

    Stage ternaryStage = new Stage();
    ternaryStage.setTitle("For new admins");
    ternaryStage.setScene(new Scene(registerAdmin, 600, 400));
    ternaryStage.show();

    Stage forthStage = new Stage();
    forthStage.setTitle("New customers register here");
    forthStage.setScene(new Scene(registerCustomer, 600, 400));
    forthStage.show();

    Stage pentaStage = new Stage();
    pentaStage.setTitle("Register new company here");
    pentaStage.setScene(new Scene(registerCompany, 600, 400));
    pentaStage.show();

    Stage hexaStage = new Stage();
    hexaStage.setTitle("Create flats here");
    hexaStage.setScene(new Scene(registerFlat, 600, 400));
    hexaStage.show();*/

}


public static void main(String[] args) {
    launch(args);
}

}

Below I have included my controller code:

package realEstateApplication.controllers;

import javafx.fxml.FXML;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 * Created by PriteshJ on 4/16/16.
 */
public class loginViewController extends Application {
    @FXML private TextField username_tField;
    @FXML private PasswordField password_tField;
    @FXML private Button login_Button;
    @FXML private Button signUp_Button;


    public loginViewController() {
        System.out.println("Login Controller constructor invoked ...\n");
    }

    @Override
    public void start(Stage loginStage) throws Exception {
        Parent loginRoot = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));
        if(loginRoot == null)
            System.out.println("something weird happened .... ");
        loginStage.setTitle("Login");
        loginStage.setScene(new Scene(loginRoot, 600, 400));
        loginStage.initModality(Modality.WINDOW_MODAL);
        loginStage.show();
    }

}

(So I tried to pass a reference to Parent variable which points to my fxml file which is within scope of start method since my Main class extends Application (javafx), that gave me NullPointerException. Tried to make it static but that didn't work either. Then tried approach I posted now , it gives no errors but neither it works.)

Well I am trying to think from MVC perspective in JavaFX. So I got a loginViewController which I rely to completely own login.fxml and do any operations on it. I got a main class where JavaFX application starts but it can only construct my view from an object of my viewController. I went through lots of examples, but so far I never came across an example that shows how to do above stuff that I want to.

Looking forward to suggestions.

  • I suggest you have a look at [this answer](http://stackoverflow.com/a/33304137/4185959). – Itai Apr 17 '16 at 04:08
  • Possible duplicate of [Javafx - Can application class be the controller class](http://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class) – Itai Apr 17 '16 at 04:09
  • I was not calling initialize in my controller class. That was the issue. I managed to solve the issue. Thanks so much for the help :) – Pritesh Lakshmikant Jakhotia Apr 18 '16 at 14:47

0 Answers0