繁体   English   中英

JTable和自定义TableModel不能按预期运行

[英]JTable & custom TableModel not acting as expected

因此,当我尝试在程序中实现JTable时,我一直在阅读Java的“如何使用表” 我想要的是从数据库中获取字符串值的列表,然后按列名和行名对它们进行排序。 现在,我知道没有默认的行标题,就像列的行标题一样,因此我一直在通过将第一列设为“行标题”来回避这一点。 因此,我决定为我的Jtable创建一个自定义表模型,以便对数据进行正确排序(数据分别存储在“字符串向量的向量”和列/行名的向量中,作为“字符串向量”),但是我一直在运行成问题。 最初,我收到了一大堆由于索引错误导致的数组,因此我添加了代码,向我展示了将表模型中的数据放入Jtable的位置。 所以这是我的Jpanel中初始化我的Jtable的代码

    //other GUI stuff above: buttons, labels, etc.
    Vector<String> tableColNames = new Vector<String>(1);
    Vector<String> tableRowNames = new Vector<String>(1); 
    Vector<Vector<String>> tableData = new Vector<Vector<String>>(1,1); //<row, col>

    for(int i = 0; i <50; i++){
        tableData.insertElementAt(new Vector<String>(), i);
        for(int b = 0; b<5; b++){
            tableData.elementAt(i).insertElementAt("TestData", b);
        }
        tableRowNames.insertElementAt("TestRowNames", i);
    }

    for(int a = 0; a<5; a++){
        tableColNames.insertElementAt("TestColNames", a);
    }

    System.out.println(tableData.toString());


    table = new JTable(new SemesterTableModel(tableColNames, tableRowNames, tableData));
    table.getColumnModel().getColumn(0).setCellRenderer(new JRowHeader());//Makeshift "rowHeader"
    table.setRowHeight(30);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setBackground(Color.LIGHT_GRAY);



    JScrollPane scrollPane = new JScrollPane(table);
    //scrollPane.setBackground(Color.BLUE);
    springLayout.putConstraint(SpringLayout.WEST, scrollPane, 180, SpringLayout.WEST, this);
    springLayout.putConstraint(SpringLayout.NORTH, lblCumGPA, 30, SpringLayout.NORTH, scrollPane);
    springLayout.putConstraint(SpringLayout.EAST, comboBoxSemester, -15, SpringLayout.WEST, scrollPane);
    springLayout.putConstraint(SpringLayout.EAST, lblCumGPA, -15, SpringLayout.WEST, scrollPane);
    springLayout.putConstraint(SpringLayout.EAST, lblSemGPA, -15, SpringLayout.WEST, scrollPane);
    springLayout.putConstraint(SpringLayout.SOUTH, btnRemoveSemester, 0, SpringLayout.SOUTH, scrollPane);
    springLayout.putConstraint(SpringLayout.EAST, scrollPane, -15, SpringLayout.EAST, this);
    springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 15, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -15, SpringLayout.SOUTH, this);

    add(scrollPane);

    table.setFillsViewportHeight(true);

因此,在这里您看到我用SemesterTableModel初始化了Jtable并将3个向量作为SemesterTableModel的参数传递。 然后在下面传递:

public class SemesterTableModel extends AbstractTableModel {

private Vector<String> colNames, rowNames;
private Vector<Vector<String>> data;

public SemesterTableModel() {
    colNames = new Vector<String>(1);
    rowNames = new Vector<String>(1);
    data = new Vector<Vector<String>>(1,1);
}

public SemesterTableModel(Vector<String> colNames, Vector<String> rowNames, Vector<Vector<String>> data){
    this.colNames = colNames;
    this.rowNames = rowNames;
    this.data = data;
}

@Override
public int getColumnCount() {
    return colNames.size();
}

@Override
public int getRowCount() {
    return rowNames.size();
}

@Override
public Object getValueAt(int col, int row) {
    if(col == 0)
        return rowNames.elementAt(row);
    else if(col >=5 || row >=5) //This is the code I added to figure out where my data was going, before I added this else if statement I was getting the array-index out of bounds errors
        return "Out of Bounds";
    else
        return data.elementAt(row).elementAt(col);
}

@Override
public boolean isCellEditable(int row, int col){
    if(col == 0 || row < 5){ //5 is an arbitrary number, I really want this to be an arbitrary variable that will be dependent on another class I haven't finished yet.
        return false;
    }
    return true;
}

}

因此,当我运行程序时,我的表如下所示。

在此处输入图片说明

自然,我希望“ TestData”介于“行标题”和列标题之间,“ TestRowNames”仅位于第一列中,然后我也有一个“ TestColNames”甚至不显示(尽管我记得正确地,我需要通过Jtable本身而不是TableModel更改列标题。

显然,我对这里的内容不了解,并且我已经将头撞在键盘上了一段时间,现在试图弄清楚这一点。 如果你们知道最新消息或有任何建议,我将为您倾听。

TableModel#getValueAt应该是int row, int col不是int col, int row ...

public Object getValueAt(int row, int col) {

您必须删除“越界”检查以使其完全起作用...因为有5行以上

仔细研究如何使用滚动窗格,以了解如何能够实现自己的行标题而无需伪造它……

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM