从 AbstractTableModel 获取列名
我无法使用构造函数 JTable(TableModel dm)
找出一些东西。
我使用 LinkedList 来管理我的数据,因此,为了显示它,我扩展了 AbstractTableModel
:
public class VolumeListTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private LinkedList<Directory> datalist;
private Object[] columnNames= {"ID", "Directory", "Wildcard"};
public VolumeListTableModel(){
}
public void setDatalist(LinkedList<Directory> temp){
this.datalist = temp;
}
public LinkedList<Directory> getDatalist(){
return (LinkedList<Directory>) this.datalist.clone();
}
public Object[] getColumnNames() {
return this.columnNames;
}
@Override
public int getColumnCount() {
return Directory.numCols;
}
@Override
public int getRowCount() {
return this.datalist.size();
}
@Override
public Object getValueAt(int row, int col) {
Directory temp = this.datalist.get(row);
switch(col){
case 0:
return temp.getId();
case 1:
return temp.getPath();
case 2:
return temp.getWildcard();
default:
return null;
}
}
我做错了什么,因为当我运行 GUI 时,我得到标记为 A,*B*,C。
I can't figure out something using the constructor JTable(TableModel dm)
.
I'm using a LinkedList to manage my data so, to display it, I extended AbstractTableModel
:
public class VolumeListTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private LinkedList<Directory> datalist;
private Object[] columnNames= {"ID", "Directory", "Wildcard"};
public VolumeListTableModel(){
}
public void setDatalist(LinkedList<Directory> temp){
this.datalist = temp;
}
public LinkedList<Directory> getDatalist(){
return (LinkedList<Directory>) this.datalist.clone();
}
public Object[] getColumnNames() {
return this.columnNames;
}
@Override
public int getColumnCount() {
return Directory.numCols;
}
@Override
public int getRowCount() {
return this.datalist.size();
}
@Override
public Object getValueAt(int row, int col) {
Directory temp = this.datalist.get(row);
switch(col){
case 0:
return temp.getId();
case 1:
return temp.getPath();
case 2:
return temp.getWildcard();
default:
return null;
}
}
I'm doing something wrong because when I run my GUI I get column names labeled A,*B*,C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AbstractTableModel 中没有名为 getColumnNames 的方法,因此我相信您的方法被忽略。您要重写的实际方法是 getColumnName 方法。
尝试将此方法添加到您的 VolumeListTableModel 类中
There is no method in AbstractTableModel called getColumnNames, so I believe your method is being ignored. The actual method you want to override is the getColumnName method.
Try adding this method to your VolumeListTableModel class
您需要重写
getColumnName
方法,在您的情况下,该方法将简单地You need to override the
getColumnName
method which in your case will simply你必须重写这个方法:
You have to Override this method :
AbstractTableModel 没有 getColumnNames 方法,但很容易实现:
AbstractTableModel does not have a getColumnNames method, but it is easy to implement: