Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

JavaFX dealing with scenes?

This is open to whoever wants to put in thoughts, but directed towards Craig Dennis. Maybe you can do an Intermediate workshop on this subject unless its already planned in future course? :)

So I am playing around with JavaFX code wondering a few different things.

1) How would one go about swapping Scenes in this primaryStage from any other controllers?

2) Once I am able to load a scene from any controller, what if I wanted to keep some type of static menus in place and only change parts of scenes inside the stage instead of the whole Scene?

I found a few resources on forums in which people were making attempts at doing this but the code didn't seem like they were using Best Practices or the code led to some severe repeats.

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Chris!

Great questions! I too am a little surprised that there doesn't seem to be a standard way to do this, and I think it really depends on what you are building. There are pre-wrapped JavaFX application frameworks, but are probably overkill if you are just getting started.

My favorite style that I've seen is to build all the scenes when the application starts and storing them in an instance variable on your application, maybe in a Map so you can access them by a String name. Most apps, especially the web world call these routes.

From an javafx.fxml,FXMLLoader instance you can call getController which allows you to add a reference back to your application into the controller (just make sure you add the a method to each controller, you can make sure they implement a default interface or abstract class.)

In regards to your second question, maybe you could check out AnchorPane. This is typically what you use when you want some sort of standard chrome around the inner bits.

I'll add this idea to the Java Content Roadmap. Anything else to add?

Hope that helps! What are you looking to build?

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Ah ok, I think I have seen this style you are referring to.

AnchorPane is definitely my favorite pane. Usually the one I start with before adding other elements or panes around or on top of it. The concept of switching them out was confusing me, at least the proper ways of pointing to the same place when you want to make lots of buttons point to different controllers that change out scenes.

Right now I am not building anything specific, just playing I guess. But If I do get advanced enough, there is something I would eventually like to build for a family member to help track things around the house and with her business. Hopefully I can get to a point where I can integrate database functionality into it. :)

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

I apparently need to spend more time on Trello, this is the 2nd time I didnt know that a Roadmap existed on there for Treehouse.

I remember when I first signed up on here there used to actually be a section for Roadmaps. I was here before all the new changes, I feel so old. ;)

more to add? Well like I said, once I get far enough into JavaFX. Interfacing with a database like MySQL through the Application would be interesting. But from the looks of the Roadmap. You have quite a bit of even more interesting ones coming up, actually kind of excited.

And it looks like someone already requested some Play Framework. Oh man! Though REST API may be a bit advanced to start with.