gpt4 book ai didi

java - getColumnName 无法正常工作

转载 作者:行者123 更新时间:2023-12-02 05:40:42 24 4
gpt4 key购买 nike

我正在为 TableModel 使用一个新类,它扩展了 AbstractTableModel,但是当我尝试添加标题行时,它没有显示。这是代码:

 public class StudentTableModel extends AbstractTableModel {

private ArrayList<Student> student;
private ArrayList<Group> groups;
private int tableType=2;
public String []coluNamess = {
"id","name",
"last name",
" birth date",
"phone","adress",
"class"
};
public String []coluNamesg = {
"id","title",
"level","num"
};

public String getColumnName(int column ) {
if(tableType==1) {
return this.coluNamess[column];
} else {
return this.coluNamesg[column];
}
}

public int getColumnCount() {
if(tableType==1) {
return 7;
} else {
return 4;
}
}

public int getRowCount() {
if(tableType==1) {
return student.size();
} else {
return groups.size();
}
}

public String getValueAt(int row, int colu) {
if(tableType==1) {
Student studentm=student.get(row);
switch(colu) {
case 0:
return Double.toString((double)studentm.getId());
case 1:
return studentm.getFname();
case 2:
return studentm.getLname();
case 3:
return studentm.getB_date();
case 4:
return studentm.getPhone();
case 5:
return studentm.getAdress();
case 6:
return Double.toString((double)studentm.getGroupId());
}
} else if(tableType==2) {
Group group=groups.get(row);
switch(colu) {
case 0:
return Double.toString((double)group.getId());
case 1:
return group.getTitle();
case 2:
return group.getLevel();
case 3:
return Double.toString((double)group.getTime());
}
}
return null;
}

public void setStudent(ArrayList<Student> students) {
this.student= students;
}

public void setGroup(ArrayList<Group> groups) {
this.groups= groups;
}

public void setType(int type) {
this.tableType=type;
}
}

标题未出现,但显示内容。我不知道问题出在哪里!!!!

最佳答案

向滚动 Pane 添加一个表格,即:new JScrollPane(table)。请参阅Adding a Table to a Container了解更多详情:

The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled.

If you are using a table without a scroll pane, then you must get the table header component and place it yourself.

关于java - getColumnName 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com