我想这不是关于实际需要在 FXML 中设计一个列,而是更多关于理解 FXML 和控制器类如何交互。所以我在 FXML 中定义了一个表,如下所示:
<TableView fx:id="UserList" layoutX="14.0" layoutY="34.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn editable="false" prefWidth="75.0" text="Profile" fx:id="ProfileNameColumn" />
</columns>
</TableView>
并想用主类中名为 getUsers() 的方法中的用户名填充它,我这样做是这样的:
@FXML private TableView<User> UserList;
@FXML private TableColumn<User, String> ProfileNameColumn;
public void setApp(EncryptSyncGui application){
this.application = application;
ObservableList<User> data = FXCollections.observableList(application.getUsers());
ProfileNameColumn = new TableColumn<User, String>("Profile");
ProfileNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("name"));
UserList.getColumns().add((TableColumn<User, String>) ProfileNameColumn);
UserList.setItems(data);
}
但不是用 fx:ID ProfileNameColumn 填充现有列,而是创建一个全新的列。要在 FXML 中定义列,我还必须在 FXML 中填充它吗?如果是这样,我将如何从 FXML 中的主类调用该方法?先谢了。