#How to swap scenes properly using FXML - JavaFX

1 messages · Page 1 of 1 (latest)

azure bridge
#

I took a quick look at this blog / tutorial https://www.pragmaticcoding.ca/javafx/swap-scenes
but it shows the UI in Java and im using FXML, how would i make the java UI part with FXML?

PragmaticCoding

Taking a look at the three most common design patterns for building systems with user interfaces. How are they different? Which one is best? Is there anything better?

dark ridgeBOT
#

<@&987246487241105418> please have a look, thanks.

gritty flower
#

interesting approach, but I would do it differently
to answer your question:

but it shows the UI in Java and im using FXML, how would i make the java UI part with FXML?
this is the point that uses java instead of fxml:

final Scene scene1 = new Scene(new VBox());
final Scene scene2 = new Scene(new VBox());

instead of simply doing new Scene you just use fxmlloader to load the scene

#

but well it wont work with the implementation

#

like the setRoot call wont work as you would want it

azure bridge
#

I havent touched this apart from the name and the size, but this is the default method for the stage

public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/SettingsView.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 240, 80);
        stage.setTitle("Pomodoro Timer");
        stage.setScene(scene);
        stage.show();
    }
gritty flower
#

I am really interested in answering your question, including the mvc questions, but I simply dont have the time right now

gritty flower
#

and therefore you kinda need to understand what its actually doing

#

like you dont need two classes Layout1Builder and Layout2Builder

#

with fxml you would just need one

#

which loads the fxml in the build() call

azure bridge
#

would you need/want 2 FXML files

#

or preferable just 1 FXML file for both scenes?

gritty flower
#

2 fxml

#

but the builder got the same logic for both, just for different files

#

so you dont need to define two builder

#

but as I said I would initially do it differently

#

but there are many ways

azure bridge
#

i might as well just make the 2 scenes and them see how i could combine them

#

or switch between them

gritty flower
#
public class FxmlBuilder implements Builder<Region> {
    
    private final String file;
    private final Runnable sceneSwapper;

    public FxmlBuilder(String file, Runnable sceneSwapper) { // might want to use a different type for the filename
        this.file = file;
        this.sceneSwapper = sceneSwapper;
    }

    @Override
    public Region build() {
        // load fxml using the file name, get the button and set the on action, then return the region returned by it
        return ...;
    }
}
#

something like that

azure bridge
#

so this would be the scenemanager or just only the FXML builder?

gritty flower
#

this is just the Layout1Builder, but for fxml files in general with the intention to have swapping scenes abilities

azure bridge
gritty flower
#

well therefore you need to know java

#

what exactly isnt clear for you?

#

do you know what a class is? a constructor? implementing an interface? overriding methods?

azure bridge
#

mostly implements Builder<Region> this part

#

where does the Builder come from?

gritty flower
#

its an interface that javafx provides

#

javafx.util.Builder<T>

#

but you dont need it tbh

azure bridge
#

oowh is that the scene builder?

gritty flower
#

for your usage the class will work fine like this:

public class FxmlBuilder {
    
    private final String file;
    private final Runnable sceneSwapper;

    public FxmlBuilder(String file, Runnable sceneSwapper) { // might want to use a different type for the filename
        this.file = file;
        this.sceneSwapper = sceneSwapper;
    }

    public Region build() {
        // load fxml using the file name, get the button and set the on action, then return the region returned by it
        return ...;
    }
}
gritty flower
#

its just an interface

#

but you dont take advantage of the FxmlBuilder being a subclass of it, so you dont need it