user1714691
user1714691

Reputation: 31

javafx - updating values in fxml object which was dynamically loaded on button click

I am creating a simple Weather Application

Here goes the details of my question :-

I have a main Controller (GeoWeatherMain.java). When Appl is run, this class(GeoWeatherMain.class) gets loaded which loads an fxml file. Below is code for it :-

Parent root = FXMLLoader.load(getClass().getResource("GeoWeatherMainUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

Now, GeoWeatherMainUI.fxml has BorderPane object implementation. It consists of a button(on the left pane) which onclick loads another fxml file inside center pane. The skeleton of GeoWeatherMainUI.fxml looks like below:-

<BorderPane fx:id="MainBody" prefHeight="736.0" prefWidth="1140.0" xmlns:fx="http://javafx.com/fxml" fx:controller="geoweather.GeoWeatherUIActionHandlers">
    .
    .
    <Button layoutX="91.0" layoutY="67.0" mnemonicParsing="false" onAction="#getCurrAndForecastWeatherCond" text="Get Weather Details" />
    .
    .
<BorderPane>

Now GeoWeatherUIActionHandlers.java is another controller which takes care of different button action events.Below is its complete code

public class GeoWeatherUIActionHandlers implements Initializable{
    @FXML
    BorderPane MainBody;
    @FXML
    Label LocName;/*LocName is fx id for a label*/

    @FXML
    private void getCurrAndForecastWeatherCond(ActionEvent event){
        try{
            Pane centerPane = FXMLLoader.load(getClass().getResource("WeatherDetails.fxml"));
            MainBody.setCenter(centerPane);
            /*LocName.setText("xyz");*/
        }catch (IOException ex){
            TextArea excep = new TextArea(ex.toString());
            MainBody.setCenter(excep);
        }catch(Exception e){
            TextArea excep = new TextArea("Inside Exception : \n" + e.toString());
            MainBody.setCenter(excep);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb){}
}

Now, if I want to update the loaded WeatherDetails.fxml file with new values, how to do that? I tried as in the above commented code.(LocName.setText("xyz")). But, it didn't work (giving NullPointerException).

I went through complete documentation of javafx @ docs.oracle.com. No luck. Here also I didn't get the answer. Please guide.

Upvotes: 2

Views: 3207

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

If the LocName is located inside WeatherDetails.fxml then it is excepted behavior that the LocName will be null. Because @FXML Label LocName; is defined in GeoWeatherUIActionHandlers.java which is a controller of GeoWeatherMainUI.fxml. Move LocName from WeatherDetails to GeoWeatherMainUI FXML file and see where you are still getting the error or not.

If your aim is to set the text of the label that is inside WeatherDetails.fxml, then do this work

  1. in the controller of WeatherDetails similar to current GeoWeatherUIActionHandlers, or

  2. in GeoWeatherUIActionHandlers, after loading the WeatherDetails.fxml

    • get the label by id (not fx:id) from centerPane with ((Label)centerPane.lookup("#myLabel")).setText("xyz"), or
    • get the controller of WeatherDetails and call getLocName().setText("xyz"). Assuming getter method exists in a controller class.

Upvotes: 1

Related Questions