Java 为什么这段代码创建一个新的TableColumn而不是填充FXML中定义的现有TableColumn?
我想这并不是说实际上需要在FXML中设计一个列,而是更多地理解FXML和控制器类是如何交互的。 因此,我在FXML中定义了如下表:Java 为什么这段代码创建一个新的TableColumn而不是填充FXML中定义的现有TableColumn?,java,javafx-2,Java,Javafx 2,我想这并不是说实际上需要在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="
<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>
@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);
}
我想用主类getUsers()中的一个方法中的用户名来填充它,我喜欢这样做:
<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>
@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);
}
@FXML private TableView用户列表;
@FXML私有TableColumn ProfileNameColumn;
公共void setApp(EncryptSyncGui应用程序){
这个应用程序=应用程序;
ObservableList data=FXCollections.ObservableList(application.getUsers());
ProfileNameColumn=新的TableColumn(“Profile”);
ProfileNameColumn.setCellValueFactory(新属性ValueFactory(“名称”);
UserList.getColumns().add((TableColumn)ProfileNameColumn);
UserList.setItems(数据);
}
但是,它不是用fx:ID ProfileNameColumn填充现有列,而是创建一个全新的列。要在FXML中定义列,我还必须在FXML中填充它吗?如果是这样,我将如何从FXML中的主类调用该方法?
先发制人的感谢。事实上,一个新的专栏被创建了——没有什么令人惊讶的 尝试,而不是
ProfileNameColumn = new TableColumn<User, String>("Profile");
移除
UserList.getColumns().add((TableColumn<User, String>) ProfileNameColumn);
UserList.getColumns().add((TableColumn)ProfileNameColumn);
您实际上创建了一个新列—这并不奇怪。请尝试,而不是ProfileNameColumn=new TableColumn(“Profile”);,写入ProfileNameColumn=UserList.getColumns().get(0);并删除UserList.getColumns().add((TableColumn)ProfileNameColumn);或者像那样。顺便说一句,类的字段应该从小写字母开始:userList,profileNameColumn…感谢您的帮助,这非常有效。谢谢你给我关于类字段的建议,虽然我确实花了大约半个小时,因为我忘了重构FXML文件d'oh中的fx:id值。