2015-01-14 24 views
1

所以我一直在阅读Java的"How to use Tables",因为我正试图在我的程序中实现一个JTable。我想要的是从数据库中获取字符串值列表,然后按列名和行名对它们进行排序。现在我知道没有默认行标题,就像列的标题一样,所以我通过将第一列设置为“行标题”来回避这一问题。于是我决定为我的Jtable创建一个自定义表模型,以便正确地对数据进行排序(数据存储在字符串矢量的向量中(作为Vector的字符串单独存储在列向量中),但我一直在运行成问题。起初,我从索引错误中接收了大量数组,所以我在代码中添加了一些代码,这些代码向我展示了表模型中的数据放在Jtable中的位置。 因此,这里是从我的JPanel的代码,所以在这里你看我初始化我的JTable我SemesterTableModel,并通过我的3个向量作为我SemesterTableModel参数初始化我的JTableJTable和自定义TableModel没有按预期工作

//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); 

。这则通过在下面:

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

当然,我想“TESTDATA”下滑插图中的“行头”和列标题,“TestRowNames”是仅在第一列,然后我也有“TestColNames”说唐”甚至出现(尽管如果我没有记错的话,我需要通过Jtable本身而不是TableModel来更改列标题)。

显然我不是在这里理解的东西,我一直在敲我的头对着键盘一会儿,现在试图弄清楚这一点。如果你们知道发生了什么,或者有什么建议,我就会全神贯注。

回答

1

TableModel#getValueAtint row, int colint col, int row ...

public Object getValueAt(int row, int col) { 

你必须要去掉“出界”检查,以使其充分工作...因为有更多然后5行

仔细看看How to Use Scroll Panes,看看你怎么能够实现自己的行标题,而不需要假冒它...

+0

哇......我不敢相信我错过了这样一个简单的错误。 ..非常感谢,抓住它,它按照预期的方式工作。另外,我也很感谢Row Headers的建议,我一定会检查一下。 – SilverEnsign99