简体   繁体   中英

JavaFX lssue when loading fxml file and seting its Label text

I made a similar question yesterday but I think it wasnt well explained, so I wanted to ask it again but with some changes I made in my code. I apologise if i write too much but i want to make everything understandable.

So, Im making a pokemon simulator where you can catch and train pokemon. I have one main fxml file which contains buttons to access to the diferent fxml files like catch, battle, shop, bag... Yesterday i was doing the bag where all your items will be stored.

Everything is working fine and the windows are switching between them properly. The problem comes when i was trying to add a Label to each item in the bag, which was suposed to show the user the cuantity he has of each item. So i created all the Labels with no text so they are empty.

They will get filled from the information i get from a database, this thing also works properly, i connect to the db and get the item cuantity. The problem comes when i want to show that item cuantity in my bag window.

Why? Because as you can imagine, i want that when you click on the bag button, the bag file loads with all the Labels filled with the cuantity of each item. The Labels are defined on the bag fxml controller, so if i want to fill them with some text, i cant do it from my main windows which uses another controller, i need to do it throught the bag controller.

This is the code i tried to make it work(Located in main controller):

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(pane);
        anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
        cm.getCantidad();
    }

getCantidad is a function that i have in my bag controller and this is it:

public void getCantidad() {
        lblpokeballCount.setText("Cantidad: "+pokeballcount);
        lblsuperballCount.setText("Cantidad: "+superballcount);
        lblultraballCount.setText("Cantidad: "+ultraballcount);
        lblmasterballCount.setText("Cantidad: "+masterballcount);
    }

So when i try to run this function from the main controller, it returns me null pointer exception. That means the labels are not initialized but when i type first AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml")); Shoudlnt all the resources from the file be loaded? Because i creaded a button in my bag file, when on clicked runs the same function, and it works correctly because im calling it from the same controller/file.

So now i dont really know what to do, this is a proyect for school but my programing teachers have never touched javafx so they dont even know what im doing. You are my only hope. I tried to understand this post: post

But i dont understand it at all as im new to all this stuff. So guys if you can help me i would be really gratefull, thanks!

edit:

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(loader);
        controladorMochila controller = loader.<controladorMochila>getController();
        controller.getCantidad();
    }

anchorPaneContent is an anchorpane thats inside the main pane. All the buttons are in the main pane, and depending on the button you click, anchorpanecontent will change for another fxml file. I tried to do it like in the post i mentioned above. But i cant do anchorPaneContent.getChildren().setAll(loader); because it says: the node setAll is not aplicable for the arguments(FXMLLoader)

You are trying to add an FXMLLoader to an anchor pane, which will not work, since an FXMLLoader is not a visual component (it is a thing that loads FXML files).

Additionally, you are trying to get the controller from the FXMLLoader without actually loading the FXML file; this won't work because the controller class is specified in the FXML file (so the FXMLLoader doesn't know what kind of controller to create until it loads the file).

You need to load the FXML file and add the result to the anchor pane:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    anchorPaneContent.getChildren().setAll(loader.load());
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}

or, if you want to be a bit more explicit:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    Parent pane = loader.load();
    anchorPaneContent.getChildren().setAll(pane);
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM