我創建一個簡單的天氣應用JavaFX的 - 這是動態加載的按鈕點擊
在這裏,在FXML對象更新值進入我的問題的細節: -
我有一個主控制器(GeoWeatherMain.java )。當Appl運行時,這個類(GeoWeatherMain.class)被加載並載入一個fxml文件。 下面是代碼爲它: -
Parent root = FXMLLoader.load(getClass().getResource("GeoWeatherMainUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
現在,GeoWeatherMainUI.fxml有BorderPane對象實現。它包含一個按鈕(在左側窗格中),onclick在中間窗格內加載另一個fxml文件。 GeoWeatherMainUI.fxml的骨架看起來象下面這樣: -
<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>
現在GeoWeatherUIActionHandlers.java是另一個控制器,它採取不同的按鈕動作events.Below的護理的完整代碼
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){}
}
現在,如果我想用新值更新加載的WeatherDetails.fxml文件,該怎麼做?我嘗試了上面的註釋代碼(LocName.setText(「xyz」))。但是,它不起作用(給NullPointerException)。
我經歷了javafx @ docs.oracle.com的完整文檔。沒有運氣。在這裏我也沒有得到答案。請指導。
標籤LocName位於何處?在GeoWeatherMainUI.fxml或WeatherDetails.fxml中? –
嗨Uluk。對不起,因爲我沒有收到電子郵件提醒。是。 LocName位於WeatherDetails.fxml內 – user1714691