我一直在閱讀和我怎麼能叫存儲和調用/特定類的方法從一個數組
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