我有两个控制器
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.java
public class FXMLDocumentController implements Initializable {
private FXMLDocumentController documentController; //updated
@FXML
public TableView<Customer> customerTable;
@FXML
public TableColumn<Customer, String> custname;
@FXML
public TableColumn<Customer, String> city;
@Override
public void initialize(URL url, ResourceBundle rb) { //updated
custname.setCellValueFactory(new PropertyValueFactory<>("name"));
city.setCellValueFactory(new PropertyValueFactory<>("city"));
}
人到中年有点甜
相关分类