• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Trying to understand JavaFX Event Dispatch Chain

 
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to pass an action event, e.g. Button clicked, from a controller nestled 3 levels deep in my MVC folder to the "Main" and then from the "Main", I have a method which sends message to add text to a text area in a scene nested 2 levels deep in the MVC folder.

It seems I cannot do it in one step i.e. from 3 levels deep directly to "Main".

BUT I can do it if they are all at the same level in the folder.

Does that mean I have to "bubble" the action event from 3 to 2, from 2 to Main? Still reading and trying out various techniques...

Please point me to some samples or more detailed explanations...

 
Rancher
Posts: 144
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IDK about FX - but it shouldn't matter how deep you nest your GUI - the GUI event system in Java work by you register a listener to it wich gets called by the EDT
btw: it helps if you post a SSCCE
 
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "two levels deep", are you referring to the number of package folders or how deeply the call hierarchy is?  It seems you are talking about packages.  I not exactly sure what you mean by "pass an action event."
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:When you say "two levels deep", are you referring to the number of package folders or how deeply the call hierarchy is?  It seems you are talking about packages.  I not exactly sure what you mean by "pass an action event."



Big whinge follows: and another  thing I don't understand about FX, even though I can hack the codes these days.
I can load the data into an ObservableList<>, Map<String, Stringg> etc.  BUT why do I need to put that into a ListView? Is it just so I can use a MultipleSelectionModel<>, which, by the way, must be explicitly turned on.

Stub code follows:

I'll come back to this after I've experimented with mode codes, this is taking too long
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ObservableList is the data structure.  The ListView is the GUI element.
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kristina Hansen wrote:IDK about FX - but it shouldn't matter how deep you nest your GUI - the GUI event system in Java work by you register a listener to it wich gets called by the EDT
btw: it helps if you post a SSCCE



I will trim the codes to isolate the problem this weekend. MVC makes for a lot of lines of codes for a business system! See whether you can recreate this MVC problem.

Basically, if the sub controllers are in other MVC directories more than one directory level deep, it throws null pointer exception (NPE) errors, all the time, everytime. But if I stuff everything into directory at most one level deep, everything works perfectly, but terrible for maintenance. In corresponding .fxml scripts,  I made sure the fx:controller = "..." is correct.


 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that this project structure works:

project
\-------controller
        \----------subcontroller


but this one doesn't?

project
\-------controller
        \----------subcontroller
                   \-------------subsubcontroller
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Are you saying that this project structure works:

project
\-------controller
        \----------subcontroller


but this one doesn't?

project
\-------controller
        \----------subcontroller
                   \-------------subsubcontroller


YES. The 1st is well behaved, but the 2nd, always an NPE.
I have created a simple JavaFX project, so you can see the problems.
Sorry its gotten voluminous, but that's javaFX and MVC

Objective:
==========
1)  write a log message to the LogsPage, regardless of the level the controller is in;
2)  push or pull a variable from any controller via MainController to another controller
What works:
===========
If only 2 levels in MVC folders i.e. 1 main and 1 sub, well behaved. I can send messages to LogsPage and I can push or pull values of variables between controllers on same level.
What does not work:
=============
If the controllers are in different sub-folders, nullpointerexception
If controllers more than 2 levels deep, cannot sent message to LogsPage, nullpointerexception
Flattening the folders as much as possible.
What I have tried so far:
===============
Put the methods from MainController into a lower-level controller, but which is still higer in the folder-hierarchy than the sub-controllers
I am beginning to think this is an inherent limmitation of JavaFX trying to implement the MVC architecture

MainApp333
==========================================


MainController.java
==========================================



AdminPageController.java
==========================================



LogPageController.java
==========================================


UpdateCoyDetailController.java
==========================================


GetCoyNameController.java
==========================================


RDateTime.java
==========================================


MainView.FXML
==========================================



AdminPage.FXML
==========================================



LogsPage.FXML
==========================================



UpdateCoyDetail.FXML
==========================================



GetCoyName.FXML
==========================================

FXFolders.JPG
[Thumbnail for FXFolders.JPG]
MVC folders for the codes
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking over your project, something doesn't seem right with MainController.  I don't think you can pull the sub-controllers in with an @FXML tag like that; you have to load them yourself.  Also, to make MainController aware of the sub-controllers you need to set up a controller factory in the main view.

Look at this project on github:

https://github.com/ksnortum/javafx-pass-data-demo

In the class PassDataDemo, I load the controllers and set the panes (it would be tabs in your case).  Then I set a controller factory to manage the panes.

See if this doesn't help you with your project.
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Looking over your project, something doesn't seem right with MainController.  I don't think you can pull the sub-controllers in with an @FXML tag like that; you have to load them yourself.  Also, to make MainController aware of the sub-controllers you need to set up a controller factory in the main view.

Look at this project on github:

https://github.com/ksnortum/javafx-pass-data-demo

In the class PassDataDemo, I load the controllers and set the panes (it would be tabs in your case).  Then I set a controller factory to manage the panes.

See if this doesn't help you with your project.



I am going to clone your demo from GitHub, hope you don't mind. Thank-you.
Also, reading through your intro, it seems I have been using MainController as the bridge to inject strings and properties into the other controllers. As said, it works for tree-1 but not tree-2.
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

AhFai Chan wrote:I am going to clone your demo from GitHub, hope you don't mind. Thank-you.


That's exactly what it's there for.

Also, reading through your intro, it seems I have been using MainController as the bridge to inject strings and properties into the other controllers. As said, it works for tree-1 but not tree-2.


Well, I'm hoping the controller factor will help.  If you decide to implement it, tell me how it goes.  If you're using git, do it on a branch so you can get back to your original code if it doesn't.
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a not about posting: it's not necessary to quote the previous post if the context is clear.  See

https://coderanch.com/t/727770/Quote
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me digest that first, I am still wobbly with javaFX.

Also, I had to de-install old and re-install the current Eclipse and lost my desktop GitHub, so no cloning.

But I did download, unzip and modified your codes. I couldn't get it working.

I am going to read up on factoryController, and if you can point me to more detailed example, much appreciated.

Finally, why do we need a callback.
 

In another question I posted here, I have and an if-then-else based on value of textfield.getText() without a Callback?

Thanks.
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

AhFai Chan wrote:But I did download, unzip and modified your codes. I couldn't get it working.


Couldn't get what you're trying to do working, or couldn't get my project working?  Describe what happened.

I am going to read up on factoryController, and if you can point me to more detailed example, much appreciated.


Googling javafx controllerfactory brings up some good information.

Finally, why do we need a callback.


In another question I posted here, I have and an if-then-else based on value of textfield.getText() without a Callback?  


I'm not sure I understand the question.  You need a Callback because that's the type that setControllerFactory() requires.
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just rebuilt my eclipse IDE with all the plugins.
I noticed you use Java 11.0.6 while I am still on Java 8. Based on the codes, there shouldn't be any problem running that.
My objective is to run your codes using your MVC directory structure and then increasing the depth of the structure.
Apologies, just managed to clone it in my rebuild-IDE, then imported it into Eclipse, but won't run, see .jpg
Throws this error:
Error: Could not find or load main class net.snortum.passdatademo.PassDataDemoLauncher


passdata-demo.JPG
Can't fiund or load main class
Can't fiund or load main class
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but the screen shot doesn't tell me much.  Either, at the command line, cd into the project folder and type mvn compile and copy and paste the outcome, or hover over the errors in Eclipse and write down the first sentence and post it here (no need to post the suggestions).  In general, screen shots are frowned upon at Coderanch.

[Edit: I just saw that you did this, sorry.]
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what is going wrong.  Try pressing Alt-F5 and rebuilding the Maven project.

This is what I did to import the project into Eclipse:

* in GitHub.com, copy the project as a zip file and download
* extract the zip file into my Eclipse workspace
* changed the folder name to remove the "-master" from the end
* created a new Java project in Eclipse
* unchecked "Use default location" and browsed for the folder
* after finishing this, I configured the project to be a Maven project

You mentioned that you imported the project, so I'm going to try that next.
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, importing was easier, and still created a working project:

* File > Import
* Select Maven > Import an existing Maven project
* Browse for folder
* Finish
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same error message whichever way I do it:

Error: Could not find or load main class net.snortum.passdatademo.PassDataDemoLauncher


Maven is supposed to help out with versioning and dependency, but Maven is a bridge too far for me at the moment, I already have a large software ecosphere and hope to postpone Maven until I am ready.
I have downloaded Maven plugin for Eclipse, but that didn't help, same error message.

I am going to check out Springboot, some here suggested that and just  googled it. Seems it has features that JavaFX does not, at least not out of the box.
 
Knute Snortum
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry it's not working for you.  I can't make it fail on my system.

Do you have m2e installed from the Eclipse Marketplace?  That's the software that integrates Maven with Eclipse -- at least it's the one I have.
 
AhFai Chan
Ranch Hand
Posts: 229
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have m2e installed from the Eclipse Marketplace.

I will try your codes one more time, without using Maven and with simplest MVC structure.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!