【发布时间】:2015-01-14 10:58:03
【问题描述】:
所以我一直在阅读 Java 的 "How to use Tables",因为我正在尝试在我的程序中实现 JTable。我想要的是从数据库中获取字符串值列表,然后按列名和行名对它们进行排序。现在我知道没有像列那样的默认行标题,所以我通过将第一列设为“行标题”来回避这一点。因此,我决定为我的 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 更改列标题)。
显然我在这里不明白什么,我一直在用我的头撞键盘一段时间,现在试图弄清楚这一点。如果你们知道发生了什么或有任何建议,我会全力以赴。
【问题讨论】:
标签: java swing jtable tablemodel