• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How to pass variables between stages (windows)?

 
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my new layout, the primaryStage is a log-in scene which creates a new stage i.e windows on successful login. The primaryStage needs to pass a few variables to the new stage (which actually does the grunt work and itself creates other new windows).
I tried using bindings and listeners between the 2 stages, but it is not working.

QUESTION: Am I on the right track using bindings and listeners??

P.S. I am pretty sure you can't use ObservableList, unless I screw that up too.
 
Marshal
Posts: 67443
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

AhFai Chan wrote:. . . Am I on the right track using bindings and listeners?? . . .

Not certain, but probably not. The variables don't belong to your windows but to your application. You should implement the business logic separately from the display. Your windows do no more than to display the results on screen. Pass the login from your login screen to the application and the application does all the processing and passes results to the other screens/Stages to display.
 
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:See this thread.



Opps, posted my reply on wrong thread.

Okay, I have been over-thinking this b/c of the challenges I had the last couple of months working for the 1st time with FX.

I've got it, its actually simply than I thought, and is in line with the sample codes you put up, need to getWindow() and then cast it to Stage object.. It is getting easier with time.

Effect persistency by using sqlLite is a good trick

Thank-you again
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:See this thread.




Have you tried this approach to persist the same data for all scenes.
https://gamedev.stackexchange.com/questions/110958/what-is-the-proper-way-to-handle-data-between-scenes

For example, I have MVC where
M=User.java (which is static)
V=LoginView.fxml
C=Login.java

and I load the data from the View's textfields to User.java, then according to the posting, I should be able to access User.java in any scene.
Since the login data is static, they shouldn't change throughout the session.
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with that link is that the programming examples are in C#.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:The problem with that link is that the programming examples are in C#.



The more I think about it... I think your suggestion to use Sqlite to persist data across multiple scenes is the way to go.
In my cloud database, the users will have a table to store their settings and preferences which the FX scenes need to access e.g. preference for °C over °F etc
The intention is to download the user settings on login and the data must persist across all scenes.

When I was coding with Android, Sqlite is part of the Android, that was easy.

My greatest concern is Java does not include Sqlite. Also, I would like the system to be "software as a service", wouldn't adding Sqllite add to a bigger software ecosystem (which is quite big even now)??

Oracle has made FX a bit complicated.
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a build system like Maven or Gradle, adding Sqlite is pretty easy.

One last thing before you go with Sqlite: If your data is system-wide, that is, you don't need instance data, you could use a class with a static Properties field and get your data from that.  Even with Sqlite, keeping the instances separate will be difficult. If you need instance data then I think dependency injection is the way to go.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:If you have a build system like Maven or Gradle, adding Sqlite is pretty easy.

One last thing before you go with Sqlite: If your data is system-wide, that is, you don't need instance data, you could use a class with a static Properties field and get your data from that.  Even with Sqlite, keeping the instances separate will be difficult. If you need instance data then I think dependency injection is the way to go.



About DI, my eye glazed over halfway through this write-up on DI
DI
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a great article, but it's a little bit too much for a small project.  I like the way the DI mechanism helps testing.
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you already have a cloud DB, I don't see the need for SQLite.  You could just get the info as you need it from the cloud DB.  Or, you could create a Config object with a static field like this:
The key would be the user ID (or whatever is unique about the user) and the value would be a Properties class.  You would load the user's config data after validating the user.  Then from each Scene you could write
Now you can use properties to get the user data you need.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:If you already have a cloud DB, I don't see the need for SQLite.  You could just get the info as you need it from the cloud DB.  Or, you could create a Config object with a static field like this:
The key would be the user ID (or whatever is unique about the user) and the value would be a Properties class.  You would load the user's config data after validating the user.  Then from each Scene you could write
Now you can use properties to get the user data you need.



Thanks, will try that.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:If you already have a cloud DB, I don't see the need for SQLite.  You could just get the info as you need it from the cloud DB.  Or, you could create a Config object with a static field like this:
The key would be the user ID (or whatever is unique about the user) and the value would be a Properties class.  You would load the user's config data after validating the user.  Then from each Scene you could write
Now you can use properties to get the user data you need.



Feedback.
Yes, that worked well.
For now, I only need one user's data for now because I haven't upscaled my system yet.
When I have to handle multiple users, I'll use the
Thanks for your input and patience.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand why my first approach did not work
This is what I did, using the MVC architecture:

1)  Create the VIEW e.g. UserView.fxml
2)  Create the MODEL e.g. User. java <================== I made this a static class because I thought if I do that, I can access the same data from all other scenes!
3)  Create the corresponding CONTROLLER e.g. UserControllor.java

Nope, the static User class not accessible by other controllers, they all threw NPE!
I mean, it's just one Java class accessing methods in another Java class. What's the deal with FX?
Using .properties work.

Wish I know more...

 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, there is nothing called a static class in Java.  Do you mean that all the methods are static?  This kind of class is not suitable for a model, where you need several instantiations of the class (objects).  I'm not sure why you get an NPE but it sounds like you are trying to get a User object then "have it available" to other classes.  You can only pass an instance of a class to another via injection.  The object reference (the variable) is only "seen" within the scope it's created in.
 
AhFai Chan
Ranch Hand
Posts: 209
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:First, there is nothing called a static class in Java.  Do you mean that all the methods are static?  This kind of class is not suitable for a model, where you need several instantiations of the class (objects).  I'm not sure why you get an NPE but it sounds like you are trying to get a User object then "have it available" to other classes.  You can only pass an instance of a class to another via injection.  The object reference (the variable) is only "seen" within the scope it's created in.


Sorry about my terminology.
I am going to revisit this thread and memorize that by rote: The object reference (the variable) is only "seen" within the scope it's created in
Thank-you again.
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.  And if I was too terse, I apologize.  I get caught with incorrect terminology too.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!