2

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.

2
  • Where is the label LocName located? In GeoWeatherMainUI.fxml or in WeatherDetails.fxml? Commented Oct 2, 2012 at 16:43
  • Hi Uluk. Sorry for delay as I didn't get email alerts. Yes. LocName is located inside WeatherDetails.fxml Commented Oct 3, 2012 at 14:41

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Wow !!! Awesome. I chose option 2, sub-option 1. It worked!!! Reason for choosing that opt is that the UI rapidly changes for centerPane. In future, I am thinking of including more and more features. Thanks a lot bro...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.