0

So I have a JavaFX application that has many different controllers and FXML documents. It uses a "ScreenController" class to control the flow from gui to gui throughout the application. The main gui has several buttons and when one is clicked a method is called in the screen controller that loads an associated FXML doc using the standard FXMLLoader and the controller is loaded by setting the controller factory to get the bean from spring. Then the screen controller fades out the main stage, sets the newly loaded parent to scene, sets the scene to the stage and fades the stage back into view. This all works fine. But I was thinking that I would like to have a task bar, so to speak, on the left hand side of the screen that is always there to allow the user to change the gui to any other gui at any time. It would look similar to this screen shot. Example. I am looking for some basic advice on how to achieve this. I was thinking some sort of grid pane maybe with a slim column on the left and a bigger main column on the right that would house the scene that is loaded by clicking an icon on the left. If I were using a single controller I know this could be achieved by using a stack pane and swapping through the stack when an icon is clicked. But I have controller/fxml document pair for each gui. Is it possible to load this into said grid pane? Or am I approaching this incorrectly?

This is merely an engineering question. I'm not looking for exact code, just advice on if it can be achieved or not.

Ct_Laake
  • 1
  • 2
  • 1
    Given all the other complexities that you have managed to overcome, this doesn’t seem as though it should be too difficult for you. Which portion of the problem do you have an issue with? Can you explain the difficulty with more detail or precision? – jewelsea Aug 13 '23 at 07:34
  • 2
    Are you asking [how to load fxml into a child pane](https://stackoverflow.com/questions/18619394/loading-new-fxml-in-the-same-scene). Or how to [share a menu bar across app screens](https://stackoverflow.com/questions/72722769/how-do-i-display-my-menubar-across-all-my-scenes-in-javafx) Or how to create a [navigation menu](https://stackoverflow.com/questions/13556637/how-to-have-menus-website-style-navigation-links-in-java-desktop-application/13574231#13574231)? – jewelsea Aug 13 '23 at 07:56
  • 1
    Or something else? This [widgetfx vertical app tray](https://github.com/ewidgetfx/ewidgetfx/blob/master/app-launchers/LaunchTrayFX/src/main/java/org/ewidgetfx/applauncher/apptrayfx/VerticalAppTray.java)? – jewelsea Aug 13 '23 at 07:57
  • So the FXMLLoader.load() method returns a type of Parent. Did I just overlook the fact that I could have a Pane in the right side of the grid pane I mentioned, and call pane.getChildren().add(parent)? So the new parent that is loaded into the pane will still be controlled by the controller fetched by the controller factory right? Btw jewelsea, your content on this sight has helped me countless times in the past, I just wanted to say thank you. – Ct_Laake Aug 13 '23 at 11:42
  • 4
    The `FXMLLoader.load()` method is generic, so its return type is actually variable. The actual object it loads is determined by the root node of the FXML it is loading. So, eg, if the root node is a `BorderPane` its return type is `BorderPane` etc. Since in almost all use cases the root node is some subclass of `Parent`, and that’s the type required by `Scene.setRoot(…)`, it makes sense to assign it to a `Parent` reference. But `Parent` is of course a subclass of `Node`, so you can pass that to any method expecting a `Node`, such as `pane.getChildren().add(…)`. – James_D Aug 13 '23 at 12:01
  • 1
    It’s probably slightly easier to use a `BorderPane` than a `GridPane` because you can do, eg, `borderPane.setCenter(…)` which will *replace* the center node. With a `GridPane` you would need to manually remove the existing node. – James_D Aug 13 '23 at 12:04
  • I guess I was just confused about the fact that the icons that I would have on the left would be loaded initially from FXML with their own controller. Then when one is clicked it would load another gui/node which also would have its own controller. So literally speaking I would have a scene with border pane and icons on the left which would be controlled by controller A, and a node in the middle which would be controlled by controller B. This works right? But is it considered bad practice? – Ct_Laake Aug 13 '23 at 12:18
  • 1
    Yes, that works. No, it's not bad practice (it's in fact good practice). – Slaw Aug 13 '23 at 12:25
  • Cheers, guys. Thanks so much for you input. I've been working on this project for years and all your content on JavaFX has played a huge role in helping me get to where I am now. I can't tell you how much its appreciated! I'll let you know how it goes once I get it implemented. – Ct_Laake Aug 13 '23 at 13:02
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 13 '23 at 13:22
  • 1
    If it's helpful, I created this small example project a few years back to do something very similar to what I think you're look for. I called it [Left Menu Sandbox](https://github.com/nevisnor/left-menu-sandbox) – RonSiven Aug 14 '23 at 02:58
  • 1
    [Here](https://gist.github.com/james-d/bae73788a9b881cf9358cbf2d842f554)'s another example of switching content, using a slightly different approach. This example changes the entire content of the scene (using `scene.rootProperty().bind(viewManager.currentViewProperty())`), but you could just as easily do `borderPane.centerProperty().bind(...)`, etc. – James_D Aug 14 '23 at 15:08

0 Answers0