Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Closing Pane in a javafx application

    Quote Originally Posted by benji2505 View Post
    unusual setup, but you should be able to get it to work.
    which reference is pointing to null? can you post the relevant code of the Main class? Only the relevant lines, not all 500 lines of code, if possible.

    Here is the start of the code for my app.
    Java FX Code:
    public class inventoryControl extends Application {
    
    	public static Stage PRIMARY_STAGE = null;
    	private static Group ROOT;
    	final static Logger log = LogManager.getLogger(inventoryControl.class);
    	ObservableList<Product> curInventory = FXCollections.observableArrayList();
    	ObservableList<Material> curMaterialInventory = FXCollections.observableArrayList();
    	ObservableList<PFM> curPFMInventory = FXCollections.observableArrayList();
    	AnchorPane curInventoryPane = new AnchorPane();
    	newPFMController _newPFMController;
    	BorderPane applicationArea = new BorderPane();
    	InventoryDB dbCon = new InventoryDB(Database Connection info goes here);
    		
    	public inventoryControl()
    	{
    		
    		//get current parts inventory from database and store in observablelist
    		curInventory = dbCon.getAllProductInventory();
    		curMaterialInventory = dbCon.getAllMaterialInventory();
    		curPFMInventory = dbCon.getAllPFMInventory();
    	}
    	
    	
    	@Override
    	public void start(Stage primaryStage) throws IOException
    	{
    		log.info("Program launched: setting up stage...");
    		PRIMARY_STAGE = primaryStage;
    		Group root = new Group();
    		ROOT = root;
    		Scene scene = new Scene(root, 1024,850);
    		primaryStage.setScene(scene);
    				
    		applicationArea = createApplicationArea();
    			
    		root.getChildren().add(applicationArea);
    			
    		primaryStage.show();
    		
    	}
    Here is the code for my menuitem. This part of the code is inside of my menubar setup method; each menuitem has a similar code setup. I'm also getting a message from eclipse about my line of code that loads my fxml file. It is suggesting that I should be accessing the static method of the FXMLLoader in a static way. I couldn't figure out how to access the controller for this fxml file without creating the FXMLLoader variable (loader). Oh, and if you couldn't tell from my code I'm a self taught javafx coder. :)

    Java FX Code:
      MenuItem newPFM = new MenuItem("New PFM");
    		newPFM.setOnAction((actionEvent->{
    			try{
    				FXMLLoader loader = new FXMLLoader();
    				
    				Pane newPFMForm = loader.load(getClass().getResource("view/newPFM.fxml"));
    								
    				//BorderPane appArea = (BorderPane) root.getChildren().get(0);
    				
    				applicationArea.setCenter(null);
    				applicationArea.setCenter(newPFMForm);
    				
    				_newPFMController = loader.getController();
    								
    				//BorderPane.setAlignment(newPFMForm, Pos.CENTER);
    				
    				//appArea.setCenter(newPFMForm);
    				newPFMController.setBorderPane(applicationArea);				
    			}catch(IOException e){
    				MessageClass message = new MessageClass();
    				
    				Alert a = new Alert(AlertType.ERROR);
    				String title = "Exception Dialog";
    				String header = "IO Exception!";
    				String content = "Error while loading new pfm window.";
    							
    				a = message.getIOExpectionMessage(a, title, header, content, e);
    				a.showAndWait();
    			}
    		}));
    Here is the code in my controller class.
    Java FX Code:
    public void setBorderPane(BorderPane _mainBP){
    		log.info("BorderPane has been set!");
    		this.mainBP = _mainBP;
    	}
    	
    	@FXML
    	public void handleCancel()
    	{
    		Platform.runLater(new Runnable(){
    			@Override
    			public void run()
    			{
    				mainBP.getChildren().remove("newPFMForm");
    			}
    		});
    		
    	}

  2. #22
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    quick summary:

    a. You are programming a stage, the scene of that stage contains at a minimum a BorderPane and Menus. Everything is coded in JavaFX, the only exception being a the center of that BorderPane, which shall be filled with an fxml file with the help of its respective loader.

    b. There is a controller class newPFMController. it is supposed to control the graphics of the stage. It also handles certain functionality that is not related to graphics, like DB access. One instance of the Controller is used to control the fxml file, at least it is attempted.

    Is that correct so far?

    c. Right now my understanding is that there is no separate controller class for the fxml file. It should be included in the fxml file with something like
    fx:controller="view.newPFMController".

    d. you are getting an NPE on line 18, post#21, newPFM MenuItem

    Is that correct so far?

  3. #23
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    The whole structure of your code is confusing for sb that will have to edit this later on. I have listed some tips below. You don't have to follow them, but I have the feeling that things would be easier.

    1. try to keep the class structure in the MVC structure: Model contains the business logic and the Database access, View sets up the graphics and controller handles the interaction between these two. Each one of M-V-C typically has multiple classes.

    2. Stay with a programming in JavaFX or make everything in fxml. Your setup makes it unnecessarily complicated, but it is doable.

  4. #24
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    If you want to bring your model to an end as is, I would suggest the following steps.

    1. create a new class in the controller folder, let's call it FxmlController. Include it in the fxml file with fx:controller="controller.FxmlController". If you use SceneBuilder you can enter the fully qualified name of that class as controller (bottom left section). This will be the controller class for your fxml file.

    2. Give the nodes in the fxml-file that shall be accessed "from outside" an fx:id (maybe for the anchorPane =("myPane")).

    3. Wherever you want to interact with the fxml-file, you will have to do this by talking to the fxml-Controller. You can get that instance with a line like public static FxmlController myAwesomeInstance= loader.getController(); You can then access any node if you have announced it in the controller class with something like myAwesomeInstance.myPane.dogreatStuffHere();

    4. In the FXMLController class announce all nodes that you want to access, (example: "myPane" from 2.) for example: @FXML private AnchorPane myPane;

  5. #25
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Closing Pane in a javafx application

    a. You are programming a stage, the scene of that stage contains at a minimum a BorderPane and Menus. Everything is coded in JavaFX, the only exception being a the center of that BorderPane, which shall be filled with an fxml file with the help of its respective loader.

    b. There is a controller class newPFMController. it is supposed to control the graphics of the stage. It also handles certain functionality that is not related to graphics, like DB access. One instance of the Controller is used to control the fxml file, at least it is attempted.

    Is that correct so far?

    c. Right now my understanding is that there is no separate controller class for the fxml file. It should be included in the fxml file with something like
    fx:controller="view.newPFMController".

    d. you are getting an NPE on line 18, post#21, newPFM MenuItem
    a. Yes
    b. yes
    c. no, the newPFMController is also the controller class for the fxml. fx:controller="whitman.fa.inventory.controller.new PFMController" <-- That is what is in my FXML file where the controller is set.
    d. Yes, that is where I get the first NPE.

    Quote Originally Posted by benji2505 View Post
    If you want to bring your model to an end as is, I would suggest the following steps.

    1. create a new class in the controller folder, let's call it FxmlController. Include it in the fxml file with fx:controller="controller.FxmlController". If you use SceneBuilder you can enter the fully qualified name of that class as controller (bottom left section). This will be the controller class for your fxml file.

    2. Give the nodes in the fxml-file that shall be accessed "from outside" an fx:id (maybe for the anchorPane =("myPane")).

    3. Wherever you want to interact with the fxml-file, you will have to do this by talking to the fxml-Controller. You can get that instance with a line like public static FxmlController myAwesomeInstance= loader.getController(); You can then access any node if you have announced it in the controller class with something like myAwesomeInstance.myPane.dogreatStuffHere();

    4. In the FXMLController class announce all nodes that you want to access, (example: "myPane" from 2.) for example: @FXML private AnchorPane myPane;

    I want to make this program as easy to work with as possible and if there is a better way to accomplish it than I'm willing to redo most of it. I thought I was following the MVC approach to programming this inventory program. I'm not sure where it went off the rails and became more complicated.

    The whole structure of your code is confusing for sb that will have to edit this later on. I have listed some tips below. You don't have to follow them, but I have the feeling that things would be easier.

    1. try to keep the class structure in the MVC structure: Model contains the business logic and the Database access, View sets up the graphics and controller handles the interaction between these two. Each one of M-V-C typically has multiple classes.

    2. Stay with a programming in JavaFX or make everything in fxml. Your setup makes it unnecessarily complicated, but it is doable.
    So I should be doing all the business logic inside each of my model classes? I thought your model class only contained the getters,setters and properties for your ObservableLists. Here is a screenshot of my Project Tree, my current setup has each model class with getters and setters for my ObservableLists, Database Classes for each of the different types of database access, i.e Inventory manipulation, Production logs manipulation and Transactions tracking for inventory changes.

    From looking at the project tree, am I on the right track except for mixing business and DB logic with my view.

    Thanks for taking the time to help me out, I appreciate your help.

    Closing Pane in a javafx application-projecttree.jpg

  6. #26
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    What are these fxml files in the inventory view folder? Where are the corresponding controller classes?
    Is there a hierarchy within these fxml file, e.g. did you use fx:include to display multiple fxml files on the same scene?

    As reference for future posts in this thread: If I talk about controller classes I am talking about classes that control the graphics - exclusively (and in most cases for these fxml files). I saw you have a lot of classes name controller and I wanted to make sure we are on the same page.

    The NPE shows up because newPFMController is a class and not an instance ->forgot the "_"?. The fxml file should show up after line 11, though. Comment out lines 13 and 18 and see where that gets you.

    Question: Do you want to interact in any way with the newPFM.fxml file, e.g. updating, reading, action events, etc, etc.? Or are you just posting a "picture". I am asking because you have to set up a separate controller class for newPFM.fxml then.

  7. #27
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Closing Pane in a javafx application

    Quote Originally Posted by benji2505 View Post
    What are these fxml files in the inventory view folder? Where are the corresponding controller classes?
    Is there a hierarchy within these fxml file, e.g. did you use fx:include to display multiple fxml files on the same scene?
    Each FXML file is a form used for displaying, updating or inserting data into the inventory database. I started to move most of the controller class for the fxml forms to a different package(whitman.fa.inventory.controller) to make it easier for me to find a given controller. At the time I thought it was a good idea to have the views and controllers in a separate package.

    Quote Originally Posted by benji2505 View Post
    As reference for future posts in this thread: If I talk about controller classes I am talking about classes that control the graphics - exclusively (and in most cases for these fxml files). I saw you have a lot of classes name controller and I wanted to make sure we are on the same page.

    The NPE shows up because newPFMController is a class and not an instance ->forgot the "_"?. The fxml file should show up after line 11, though. Comment out lines 13 and 18 and see where that gets you.

    Question: Do you want to interact in any way with the newPFM.fxml file, e.g. updating, reading, action events, etc, etc.? Or are you just posting a "picture". I am asking because you have to set up a separate controller class for newPFM.fxml then.
    Yes, the user will be filling in data and updating, inserting or retrieving data from the inventory database. I used scenebuilder to create the fxml file for the forms used to enter or display data from the database. Example, the newPFM fxml file is the form that a user would enter the information regarding a new purchased finished material item that needs to be added to the inventory database. The controller for that fxml file has the form controls ie textfield, choicebox, datapicker and methods for adding listeners that call methods when triggered. It also has all the business logic for filling an observablelist to be passed to the database class for DB insertion. Every form(fxml file) has a corresponding controller class. I guessing that this is completely the wrong approach.

  8. #28
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    Quote Originally Posted by caraldur View Post
    Each FXML file is a form used for displaying, updating or inserting data into the inventory database. I started to move most of the controller class for the fxml forms to a different package(whitman.fa.inventory.controller) to make it easier for me to find a given controller. At the time I thought it was a good idea to have the views and controllers in a separate package.
    It is a great idea, I would do the same thing.

    Quote Originally Posted by caraldur View Post
    Yes, the user will be filling in data and updating, inserting or retrieving data from the inventory database. I used scenebuilder to create the fxml file for the forms used to enter or display data from the database. Example, the newPFM fxml file is the form that a user would enter the information regarding a new purchased finished material item that needs to be added to the inventory database. The controller for that fxml file has the form controls ie textfield, choicebox, datapicker and methods for adding listeners that call methods when triggered. It also has all the business logic for filling an observablelist to be passed to the database class for DB insertion. Every form(fxml file) has a corresponding controller class. I guessing that this is completely the wrong approach.
    No, that is the correct approach. I would do it like that. The drama with the database is a side-show right now and I would not worry about it for now.

    I am sorry to be a little detailed, but I think I have to understand the general structure to point you in the right direction.


    Ok, let's get back to the original issue and pick one specific example. So, we have a fxml file that contains the graphic and we have a controller class that controls that specific fxml file and nothing else. These 2 are hooked up in the fxml file with fx:controller="..". If you start the program, the stage loads and if you press a menuItem the fxml file loads and shows up in the center of your mainStage - BorderPane. There is no error message so far, meaning that the fxml loader has found the fxml file, the controller class and matched all (fxml) nodes to the corresponding JavaFX objects in the controller class.

    Is that where we're at?

  9. #29
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Closing Pane in a javafx application

    Quote Originally Posted by benji2505 View Post
    Ok, let's get back to the original issue and pick one specific example. So, we have a fxml file that contains the graphic and we have a controller class that controls that specific fxml file and nothing else. These 2 are hooked up in the fxml file with fx:controller="..". If you start the program, the stage loads and if you press a menuItem the fxml file loads and shows up in the center of your mainStage - BorderPane. There is no error message so far, meaning that the fxml loader has found the fxml file, the controller class and matched all (fxml) nodes to the corresponding JavaFX objects in the controller class.

    Is that where we're at?
    That is correct, I only started getting the error when I tried figure out a way to close or clear the center pane of the borderpane.

  10. #30
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    last thing left to do is to make the controller classes and the model classes talk to each other. Let's assume we have a model class "Part" that keeps instances of parts. We also have a mainStage that displays the main-Window and a controller class for that mainStage, let's call it mainStageController (pretty fancy naming right here). Furthermore we have an FXML file, let's call it fxmlPane and the respective controller fxmlPaneController.

    I will ignore the View classes mainStage and fxmlPane from now on, because they are unimportant for what comes.

    The controller classes are Singletons, they have only one instance and (in case of the fxmlPaneController and all other fxml controllers) are instantiated by the FXML Loader.

    In order to pass these instances in to the other class you have to inject these Dependencies. You can do that in multiple ways (Spring, etc.). I will try to explain the "pedestrian way".

    Let's assume you the mainStageController has a method
    public void closeGridpaneCenter(){gridPane.setCenter(null)}
    and you want to access it from the fxmlPaneController class.
    In the mainStageController class I suggest you add

    Java FX Code:
    private static FXMLPaneController fxmlPaneController;
    
    public void initialize(){
    fxmlPaneController.injectMainStageController(this);
    }
    
    public static void injectFxmlPaneController(FxmlPaneController fxmlC){
    fxmlPaneController = fxmlC;
    }
    and in the fxmlPaneController class I suggest you add

    Java FX Code:
    private static MainStageController mainStageController;
    
    @FXML private void initialize(){
    mainStageController.injectFxmlPaneController(this);
    }
    
    public static void injectMainStageController(MainStageController mainStgC){
    mainStageController = mainStgC;
    }
    Now you should be able to address the "other" class, for example in the ButtonActionHandler in the fxmlPaneController you can write mainStageController. closeGridpaneCenter(); and I think that was the original question - LOL.
    The same thing can be done with the part class, the only difference being that the part class is not a Singleton. If you send any request from the model classes to the fxmlPaneController you have to surround it with the statement that I showed you in post#17.

    I typed this code into the browser unchecked, so there might one or two bugs, but I think you get the idea. 'hope it helps.

  11. #31
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application

    I just crossed my mind that the timing might be off in your particular program structure since the fxml files are not loaded upon program start, but in the first block of code above you can just place line 4 underneath line 8 and that should take care of it.

  12. #32
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Closing Pane in a javafx application

    Taking your suggestions, I have created a main stage fxml file and setup a controller class to handle the menu item events and passing instances of the controller classes for each fxml to this class. It worked exactly as I had hope it would and I have now begun to rewrite some of the code; trying to get the database accessing method out of the controller classes and into the model classes.

    Thanks for the help benji2505, it is much appreciated. If I run into anymore problems, I'll be sure to come here and pick your brain again.

  13. #33
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Closing Pane in a javafx application


    'glad you got it to work!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Make a JavaFX application work in JavaFXML
    By s22380 in forum JavaFX
    Replies: 2
    Last Post: 08-10-2016, 11:27 PM
  2. Dialog boxes closing on EVERY application.
    By Rtme in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2011, 07:54 PM
  3. Replies: 1
    Last Post: 01-11-2011, 07:11 AM
  4. How to intall my javaFX application on mobile
    By abuhmead1987 in forum JavaFX
    Replies: 1
    Last Post: 09-30-2010, 05:27 PM
  5. problem with playing video in javafx application
    By kiranblighty in forum JavaFX
    Replies: 1
    Last Post: 02-27-2010, 12:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •