亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從第二個控制器將值傳遞到已打開的階段(第一個控制器)?

如何從第二個控制器將值傳遞到已打開的階段(第一個控制器)?

繁星淼淼 2023-09-20 17:08:55
我有兩個控制器FXMLDocumentController有TableView客戶表。NewCustomerController第二個控制器添加新行。這是我的代碼FXML文檔.fxml<AnchorPane id="AnchorPane" prefHeight="283.0" prefWidth="437.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mytableview.FXMLDocumentController">    <children>        <Button fx:id="button" layoutX="309.0" layoutY="25.0" onAction="#handleButtonAction" text="New Customer" />      <TableView fx:id="customerTable" layoutX="6.0" layoutY="61.0" prefHeight="215.0" prefWidth="426.0">        <columns>          <TableColumn fx:id="custname" prefWidth="75.0" text="Customer Name" />          <TableColumn fx:id="city" prefWidth="75.0" text="City" />        </columns>         <columnResizePolicy>            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />         </columnResizePolicy>      </TableView>    </children></AnchorPane>  新客戶.fxml<AnchorPane id="AnchorPane" prefHeight="172.0" prefWidth="209.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="com.newcustomer.NewCustomerController">   <children>      <Button layoutX="141.0" layoutY="129.0" mnemonicParsing="false" onAction="#newCustomer" text="Add" />      <TextField fx:id="cNameTextField" layoutX="14.0" layoutY="26.0" promptText="Customer Name" />      <TextField fx:id="custCityTextField" layoutX="14.0" layoutY="77.0" promptText="City" />   </children></AnchorPane>FXMLDocumentController.javapublic class FXMLDocumentController implements Initializable {private FXMLDocumentController documentController; //updated@FXMLpublic TableView<Customer> customerTable;@FXMLpublic TableColumn<Customer, String> custname;@FXMLpublic TableColumn<Customer, String> city;  @Overridepublic void initialize(URL url, ResourceBundle rb) { //updated    custname.setCellValueFactory(new PropertyValueFactory<>("name"));    city.setCellValueFactory(new PropertyValueFactory<>("city"));           }
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

談到您的實際問題,我注意到您的代碼中存在三個問題。


關于您在 newCustomer() 方法中獲得的 NPE,您啟動了 FXMLLoader 實例但未加載它。因此 getController() 為 null。要解決此問題,您需要在調用 getController() 之前先調用 load() 方法。


public void newCustomer(ActionEvent e) throws IOException {

? ? String name = cNameTextField.getText();

? ? String stringCity = custCityTextField.getText();

? ? Customer customer = new Customer(10, name, stringCity);

? ? FXMLLoader fXMLLoader = new FXMLLoader(getClass().getResource("/mytableview/FXMLDocument.fxml"));

? ? fXMLLoader.load(); // YOU ARE MISSING THIS LINE

? ? FXMLDocumentController fXMLDocumentController = fXMLLoader.<FXMLDocumentController>getController();

? ? fXMLDocumentController.inflateUI(customer); // Getting NPE at this line.

}

然而,上述修復是無用的,因為您正在創建一個未被使用的 FXMLDocumentController 的新實例(如 @kleopatra 指定的)。您必須實際傳遞要與之通信的控制器實例。您需要在 NewCustomerController 中創建該控制器的實例變量并設置它。


@FXML

private void handleButtonAction(ActionEvent event) throws IOException {

? ? FXMLLoader fXMLLoader = new FXMLLoader(getClass().getResource("/com/newcustomer/NewCustomer.fxml"));

? ? Parent parent = fXMLLoader.load();

? ? NewCustomerController controller = fXMLLoader.getController();

? ? controller.setFXMLDocumentController(this); // Pass this controller to NewCustomerController

? ? Stage stage = new Stage();

? ? Scene scene = new Scene(parent);

? ? stage.setScene(scene);

? ? stage.show();

}

NewCustomerController.java


private FXMLDocumentController fXMLDocumentController;


public void setFXMLDocumentController(FXMLDocumentController fXMLDocumentController) {

? ? this.fXMLDocumentController = fXMLDocumentController;

}


public void newCustomer(ActionEvent e) throws IOException {

? ? String name = cNameTextField.getText();

? ? String stringCity = custCityTextField.getText();

? ? Customer customer = new Customer(10, name, stringCity);

? ? fXMLDocumentController.inflateUI(customer);//You are passing to the currently loaded controller

}

最后,您只需將 CellValueFactory 設置到 TableColumns 一次,而不是每次設置客戶時。您可以將這兩行移動到initialize() 方法。


@Override

public void initialize(URL url, ResourceBundle rb) {

? ? custname.setCellValueFactory(new PropertyValueFactory<>("name"));

? ? city.setCellValueFactory(new PropertyValueFactory<>("city"));

}


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號