Javafx app with many pages


I've got a question about building a gui in javafx.

When I build a program with many pages, I mean for example - The start page contains a couple of buttons "Add user", "Drop user", "Change user data" - each of these buttons draws an different panel. And here is my question - how to organise them. When I put all panels in one class it becomes quickly huge and generates many many bugs. It's also hard to find a part of code which is broken. Should every panel be defined in a different class? But if so, there is a problem with returning from submenus to main menu.

Or another example. First panel of program is login page and the next page depends on the inserted login.

How should I organize a lot of panels? Any help would be appreciated :)

1/29/2013 5:05:57 PM

Accepted Answer

If your project is not too big, then i would suggest making a Presenter class, which would control the stage and the program flow and which shows up one of many View classes.

This is an example of a presenter class:

class Presenter {

public void showA(Stage mainStage){
    ViewA a = new ViewA();
    a.setOnBackButton(new ViewCallback(){
        public void call(){
    mainStage.setScene(new Scene(a));

public void showB(Stage mainStage){
    ViewB b = new ViewB();
    b.setOnBackButton(new ViewCallback(){
        public void call(){
    mainStage.setScene(new Scene(b));


This is example of a view body:

public class ViewA {

private ViewCallBack onBackButton = null;

public void setOnBackButton(ViewCallback callback){ onBackButton = callback; }

public void callBack() { if (onBackButton != null); }


// somewhere in your code
Button b = new Button("shoot me!");
b.setOnAction(new EventHandler<ActionEvent>(){
    public void handle(ActionEvent event){


This is the ViewCallback interface

public Interface ViewCallback {
    public void call();

You can use this simple callback interface or Callback<P,R> JavFX generic callback interface.

2/9/2013 7:39:06 AM

If you are using fxml, then you could create a separate fxml file and associated Controller class for each panel. This will separate the view from logic within a panel and separate each panel's view and logic from every other panel.

The trick then becomes when you need to actually share data between panels - for example a change user form needs to be informed of the user to be changed. In this case you need to have some kind of context information. Different ways to do this are to:

  1. Use a global store (similar to a server's http session).
  2. Integrate a dependency injection framework such as guice, spring or CDI.
  3. Create management services which control passing data between panels.
  4. Use an event bus style system for sharing data.
  5. Implement your application like a html style web app with server managed state.
  6. Use a framework like the now (unfortunately) defunct jfxflow.

For simple applications, I'd go with - "Create management services which control passing data between panels", using the following simple project as a template. The project template is a program where the "First panel of program is login page and the next page depends on the inserted login".

If you are using plain Java and no fxml, you could look at how some other Java projects which don't use fxml are organized and organize your projects similarly. For example, the JavaFX Ensemble App, the willow browser and the JavaFX Henley Sales Data App (which is downloadable as part of the same sample code package as the Ensemble App). Reviewing the Ensemble source code is probably a good place to start.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow