当前位置: 首页 > 知识库问答 >
问题:

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

邵星光
2023-03-14

因此,当我试图在程序中实现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更改列标题)。

很明显,我不明白这里的一些东西,我一直在用我的头撞键盘一段时间,现在试图弄清楚这一点。如果你们知道发生了什么或者有什么建议,我会洗耳恭听的。

共有1个答案

吉俊德
2023-03-14

tablemodel#getvalueat应该是int行,int行而不是int行,int行...

public Object getValueAt(int row, int col) {

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

请仔细研究如何使用滚动窗格,以了解如何在不需要伪造的情况下实现自己的行标头...

 类似资料:
  • 问题内容: 我正在尝试在Java GUI上实现JTable,该JTable可以使用文件中的值填充自身,并允许用户对各种单元格进行修改。由于我在Netbeans IDE中使用GUI编辑器,因此我的第一个直觉是将JTable从面板添加到表单中。但是,我很快意识到我不能在表中添加超过100行(对于我的应用程序,我需要大约500行以上)。另外,在寻找解决方案时,我注意到很多人说使用Custom Table

  • 问题内容: 美好的一天。 如果可以根据需要更改JLabel和JTextField字体大小,是否还可以更改JTable的列名和元素的字体样式(大小,外观,颜色)? 另外,我正在使用Windows的外观。 谢谢, 问题答案: 有默认值,以及和。您可以使用与键来覆盖默认。

  • 问题内容: 美好的一天。 如果可以根据需要更改JLabel和JTextField字体大小,是否还可以更改JTable的列名和元素的字体样式(大小,外观,颜色)? 另外,我正在使用Windows的外观。 谢谢, 问题答案: 有默认值,以及和。您可以使用与键来覆盖默认。

  • 问题内容: 这是我的tableModel: 现在,在传递行号之后,请勿删除行! 问题答案: 改变这个 至

  • 例如,当我进入登录页面时...sessionCreated-将一个会话添加到计数器:1 然后,当我点击log out按钮时,会话计数减少了1(这很好),但紧接着会话计数增加了1(不是预期的)。 例如,当我按下注销按钮时...sessionDestroyed-从计数器0中扣除一个会话sessionCreated-将一个会话添加到计数器1中 我再次需要帮助来理解请。 这是我的Spring安全设置...

  • 我正在使用spring Roo并希望访问Controller类中的一个bean,该类在ApplicationContext.xml中具有以下配置: 配置类本身是: 在我的Controller中,我认为一个简单的Autowired注释应该可以完成这项工作 在启动过程中,spring在setSkipWeeks方法中打印消息。不幸的是,每当我在控制器中调用config.getSkipWeeks()时,它