从 AbstractTableModel 获取列名

发布于 2024-10-02 16:13:44 字数 1363 浏览 3 评论 0原文

我无法使用构造函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫儰鉨ミ守葔 2024-10-09 16:13:44

AbstractTableModel 中没有名为 getColumnNames 的方法,因此我相信您的方法被忽略。您要重写的实际方法是 getColumnName 方法。

尝试将此方法添加到您的 VolumeListTableModel 类中

public String getColumnName(int column) {
    return columnNames[column];
}

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

public String getColumnName(int column) {
    return columnNames[column];
}
关于从前 2024-10-09 16:13:44

您需要重写 getColumnName 方法,在您的情况下,该方法将简单地

return columnNames[column];

You need to override the getColumnName method which in your case will simply

return columnNames[column];
少年亿悲伤 2024-10-09 16:13:44

你必须重写这个方法:

public String getColumnName(int column)

You have to Override this method :

public String getColumnName(int column)
说不完的你爱 2024-10-09 16:13:44

AbstractTableModel 没有 getColumnNames 方法,但很容易实现:

public class VolumeListTableModel extends AbstractTableModel {

    // [...]

    public String[] getColumnNames() {

        String[] columnNames = new String[this.getColumnCount()];

        for (int i = 0, columnCount = this.getColumnCount(); i < columnCount; i++) {    
            columnNames[i] = this.getColumnName(i);
        }

        return columnNames;

    }

    // [...]

}

AbstractTableModel does not have a getColumnNames method, but it is easy to implement:

public class VolumeListTableModel extends AbstractTableModel {

    // [...]

    public String[] getColumnNames() {

        String[] columnNames = new String[this.getColumnCount()];

        for (int i = 0, columnCount = this.getColumnCount(); i < columnCount; i++) {    
            columnNames[i] = this.getColumnName(i);
        }

        return columnNames;

    }

    // [...]

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文