我一直在阅读和我怎么能叫存储和调用/特定类的方法从一个数组
public class Fee {
int id;
String name;
double amount;
FeeCategory feeCategory; // miscellaneous, other, tuition, etc
GradeLevel gradeLevel;
SchoolYear schoolYear;
String description;
boolean isActive;
public boolean isIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
public FeeCategory getFeeCategory() {
return feeCategory;
}
public void setFeeCategory(FeeCategory feeCategory) {
this.feeCategory = feeCategory;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public GradeLevel getGradeLevel() {
return gradeLevel;
}
public void setGradeLevel(GradeLevel gradeLevel) {
this.gradeLevel = gradeLevel;
}
public SchoolYear getSchoolYear() {
return schoolYear;
}
public void setSchoolYear(SchoolYear schoolYear) {
this.schoolYear = schoolYear;
}
我有许多不同的方法getter
其setter
方法一起使用Google了几个小时。
我需要能够调用方法来填充JTable
的单元格,并使用相应的getter
方法返回的特定值。
因此,我所做的就是创建一个DefaultTableCellRenderer
public class JTableRenderer extends DefaultTableCellRenderer{
@Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int col)
{
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
if(row%2 == 0){
cellComponent.setBackground(Color.YELLOW);
}
else{
cellComponent.setBackground(Color.CYAN);
}
for(int i=0; i<table.getRowCount(); i++){
for(int j=0; j<table.getColumnCount(); j++){
if(table.getValueAt(i, j) instanceof Fee){
Fee fee = (Fee)table.getValueAt(i, j);
table.setValue(fee.getId(),i,j);
}
}
}
return cellComponent;
}
}
的问题是在for循环中,我打算使用为特定的细胞设置特定值。
正如你所看到的,它填补所有只是id
细胞,因为我不能想办法通过getId(),getName(),getAmount(),getDescription()
进行迭代。
是否有可能把所有4种方法在数组可能像
Methods[] myMethods = {getId(),getName(),getAmount(),getDescription()};
然后,
for(int i=0; i<table.getRowCount(); i++){
for(int j=0; j<table.getColumnCount(); j++){
if(table.getValueAt(i, j) instanceof Fee){
Fee fee = (Fee)table.getValueAt(i, j);
table.setValue(fee.myMethod[j],i,j);
}
}
}
我想打电话给刚刚4点getter
的方法,但不是所有的人。
任何解决方案或建议?
您可以使用您的J-循环来决定,你必须使用哪个吸气剂。你可以打开j,如果j == 0 getId(),j == 1 getName()等等。 – IQV
或者你也可以在Fee中写一个getter,它以column-ID作为参数,所以你决定在那里,必须返回哪个值。 – IQV
1)一个开关使用一个基于getXXX的列,2)Java Stream允许你使用方法引用(现在不用多少不幸),也许可以使用这些数组(像'Fee :: getId之类的东西'), – AxelH