FYI, None of these answers could help me。因此,在将其标记为重复之前,请先阅读该问题。 因此,您可能知道,Javafx tableView中表列的初始化是通过以下方式完成的:
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
statusCol.setCellValueFactory(new PropertyValueFactory<>("status"));
contactCol.setCellValueFactory(new PropertyValueFactory<>("contact"));
programmeCol.setCellValueFactory(new PropertyValueFactory<>("programme"));
packageCol.setCellValueFactory(new PropertyValueFactory<>("packages"));
purposeCol.setCellValueFactory(new PropertyValueFactory<>("purpose"));
trainerCatCol.setCellValueFactory(new PropertyValueFactory<>("trainerCategory"));
fromTimeCol.setCellValueFactory(new PropertyValueFactory<>("fromTime"));
timeToCol.setCellValueFactory(new PropertyValueFactory<>("toTime"));
payAmountCol.setCellValueFactory(new PropertyValueFactory<>("paymentAmount"));
paymentModeCol.setCellValueFactory(new PropertyValueFactory<>("paymentMode"));
serialNoCol.setCellValueFactory(new Callback<CellDataFeatures<Member, Member>, ObservableValue<Member>>() {
@SuppressWarnings("unchecked") // Just for this one statement
@Override public ObservableValue<Member> call(CellDataFeatures<Member, Member> p) {
return new ReadOnlyObjectWrapper(p.getValue());
}
});
serialNoCol.setCellFactory(new Callback<TableColumn<Member, Member>, TableCell<Member, Member>>() {
@Override public TableCell<Member, Member> call(TableColumn<Member, Member> param) {
return new TableCell<Member, Member>() {
@Override protected void updateItem(Member item, boolean empty) {
super.updateItem(item, empty);
if (this.getTableRow() != null && item != null) {
int index=this.getTableRow().getIndex()+1;
setText(index+"");
} else {
setText("");
}
}
};
}
});
serialNoCol.setSortable(false);
现在,问题是,我有很多这样的表,我想编写一个函数来简化我的工作。但是,唯一让我受阻的是类名称(在此示例中为成员),该名称对于我正在使用的所有表都不同。我为简化序列号以外的列的初始化编写的函数是:
void initCol(TableColumn [] membershipCols,String [] colValues,TableColumn serialNoCol){
for(int i=0; i< membershipCols.length; i++){
membershipCols[i].setCellValueFactory(new PropertyValueFactory<>(colValues[i]));
}
}
这就是我所谓的函数:
TableColumn [] membershipCols ={memNameCol,MemNewCol,MemRenewCol,MemProgrammeCol,MemPackageCol,MemAmountCol,MemAdmAmountCol,
CashAmtCol,CardAmtCol,ChqAmtCol,refferedCol};
String [] colValues= {"name","newMem","renew","programme","packages","amount","admAmount","card","cash","cheque","refferedBy"};
initCol(membershipCols,colValues, serialNoCol);
我唯一遇到的问题是使用不同的类初始化示例中指定的SerialNoCol。那么,有什么办法可以将Class name作为函数参数并解决此问题?