第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

繁星淼淼 2023-09-20 17:08:55
我有兩個(gè)控制器FXMLDocumentController有TableView客戶表。NewCustomerController第二個(gè)控制器添加新行。這是我的代碼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 回答

?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

談到您的實(shí)際問題,我注意到您的代碼中存在三個(gè)問題。


關(guān)于您在 newCustomer() 方法中獲得的 NPE,您啟動(dòng)了 FXMLLoader 實(shí)例但未加載它。因此 getController() 為 null。要解決此問題,您需要在調(diào)用 getController() 之前先調(diào)用 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.

}

然而,上述修復(fù)是無用的,因?yàn)槟趧?chuàng)建一個(gè)未被使用的 FXMLDocumentController 的新實(shí)例(如 @kleopatra 指定的)。您必須實(shí)際傳遞要與之通信的控制器實(shí)例。您需要在 NewCustomerController 中創(chuàng)建該控制器的實(shí)例變量并設(shè)置它。


@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 設(shè)置到 TableColumns 一次,而不是每次設(shè)置客戶時(shí)。您可以將這兩行移動(dòng)到initialize() 方法。


@Override

public void initialize(URL url, ResourceBundle rb) {

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

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

}


查看完整回答
反對(duì) 回復(fù) 2023-09-20
  • 1 回答
  • 0 關(guān)注
  • 99 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)