0

So i'm working on a pet project, essentially a digitalisation of a do it yourself role playing adventure book.

I switched to using Scenebuilder because of the freedom it allows when crafting a GUI. I'm having trouble binding the data to the screen. It seems the objects I am calling either stop existing or are not the ones i need.

I am using a SQLite database for my data, and that seems to work fine. I am using Maven to import the things i need, this also seems to work fine however this requires me to use

public class DdAppLauncher {
    public static void main(String[] args) {
        DdApp2.main(args);
    }
}

into ->

public class DdApp2 extends Application {


    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/mainWindow.fxml"));


        stage.setTitle("Deathtrap Dungeon");
        stage.setScene(new Scene (root, 800,600));
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);

    }
}

This brings up the titlescreen that contains a start button which is handled by the following controller.

public class MainController {

    LocationsPool loc;
    Location currentLocation;

    @FXML
    private ListView<String> inventory;

    @FXML
    private Text locationDescription;

    @FXML
    private Text descrA;
    @FXML
    private Text descrB;
    @FXML
    private Text descrC;
    @FXML
    private Text descrD;
    @FXML
    private Text descrE;
    @FXML
    private Text descrF;


    @FXML
    public void startButtonClicked(ActionEvent event){

        try {
            System.out.println("Starting game");
            Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();

            // Swap screen
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/gameWindow.fxml"));
            stage.setScene(new Scene(root,800,600));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Setup
        loc = new LocationsPool();
        currentLocation = loc.getLocation(1);


        System.out.println(currentLocation.getDescription());

        locationDescription = new Text(currentLocation.getDescription());
        System.out.println(locationDescription.getText());
        System.out.println(locationDescription);

    }


    @FXML
    public void handleButtonA(){
        System.out.println(currentLocation==null);

    }
}

Output on console -> starting game

The clamour... (works)

The clamour... (works)

On button press ->

True

So it seems the app "forgets" fields when it runs? or is it the controller that stop existing and is remade?

Furthermore when trying to bind data to fields with fx:id, it doesn't seem to link those fields to anything until i press that button.

Am i structuring this all wrong? What am i not getting? The final product should have the description loaded and all the choices loaded for that location. Then on a selection should load up a new location and new choices so the text needs to be updated.

Thanks in advance. Kev

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • You replace the scene in the `startButtonClicked` method with a new one. By default `FXMLLoader` creates a new controller instance every time you load a fxml (assuming the fxml contains the `fx:controller` attribute). Even loading the same fxml multiple times will result in different controllers. Furthermore note that replacing the value of a field that was injected won't result in a change of a scene; The loader simply makes the nodes available by injecting them. Any assingments to a field in a controller have the same effects as assignments in any other class... – fabian Oct 01 '19 at 20:48
  • Even though it's not 100% what you're asking for, the following thread should shed some light on the situation: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Oct 01 '19 at 20:49

0 Answers0