JTable&自定义TableModel未按预期运行

时间:2015-01-14 02:47:27

标签: java swing jtable tablemodel

所以我一直在阅读Java "How to use Tables",因为我正在尝试将JTable实现到我的程序中。我想要的是从数据库中获取一个String值列表,然后按列和行名称对它们进行排序。现在我知道没有像列那样的默认行标题,所以我通过使我的第一列成为"行标题"来回避这一点。所以我决定为我的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;
}

}

所以现在当我运行我的程序时,我的表看起来像这样。

enter image description here

当然,我想要&#34; TestData&#34;落入&#34;行标题&#34;并且列Headers,&#34; TestRowNames&#34;仅在第一列中,然后我还有&#34; TestColNames&#34;甚至不会出现(但如果我没记错的话,我需要通过Jtable本身而不是TableModel来更改列标题。)

显然我在这里并没有理解某些东西,而且我一直在敲击键盘一段时间,现在试图解决这个问题。如果你们知道了什么,或者有任何建议我都会听到。

1 个答案:

答案 0 :(得分:1)

TableModel#getValueAt应为int row, int col而不是int col, int row ...

public Object getValueAt(int row, int col) {

你必须删除“越界”检查以使其完全正常工作...因为有超过5行

仔细查看How to Use Scroll Panes,了解如何实现自己的行标题,而无需伪造它......