JTable 和自定义 TableModel 未按预期运行
JTable & custom TableModel not acting as expected
所以我一直在通读 Java 的 "How to use Tables",因为我试图在我的程序中实现 JTable。我想要的是从数据库中获取一个字符串值列表,然后按列名和行名对它们进行排序。现在我知道没有像列那样的默认行 Header,所以我一直通过将第一列设为 "row header" 来回避这一点。因此,我决定为我的 Jtable 创建一个自定义 Table 模型,以便正确排序数据(数据存储在 Vector of Vectors of Strings & column/row names 作为Vector of Strings separately) 但我 运行 遇到的只是问题。起初我收到一大堆索引错误的数组,所以我添加了代码来显示 table 模型中的数据被放入 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);
所以在这里你看到我用我的学期Table模型初始化我的 Jtable 并将我的 3 个向量作为我的学期Table模型的参数传递。然后在下面传递:
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;
}
}
所以现在当我 运行 我的程序时,我的 table 看起来像这样。
自然我希望 "TestData" 位于 "row Headers" 和列 Header 之间, "TestRowNames" 仅在第一列中,然后我也有"TestColNames" 甚至没有出现(如果我没记错的话我需要通过 Jtable 本身而不是 Table 模型更改列 Header)。 =14=]
显然我不理解这里的某些东西,我一直在用头敲键盘一段时间,现在试图弄清楚这个问题。如果你们知道发生了什么事或有任何建议,我会洗耳恭听。
TableModel#getValueAt
应该是 int row, int col
而不是 int col, int row
...
public Object getValueAt(int row, int col) {
您必须删除 "out of bounds" 检查才能使其完全正常工作...因为有超过 5 行
仔细查看 How to Use Scroll Panes 了解如何实现您自己的行 header 而无需伪造它...
所以我一直在通读 Java 的 "How to use Tables",因为我试图在我的程序中实现 JTable。我想要的是从数据库中获取一个字符串值列表,然后按列名和行名对它们进行排序。现在我知道没有像列那样的默认行 Header,所以我一直通过将第一列设为 "row header" 来回避这一点。因此,我决定为我的 Jtable 创建一个自定义 Table 模型,以便正确排序数据(数据存储在 Vector of Vectors of Strings & column/row names 作为Vector of Strings separately) 但我 运行 遇到的只是问题。起初我收到一大堆索引错误的数组,所以我添加了代码来显示 table 模型中的数据被放入 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);
所以在这里你看到我用我的学期Table模型初始化我的 Jtable 并将我的 3 个向量作为我的学期Table模型的参数传递。然后在下面传递:
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;
}
}
所以现在当我 运行 我的程序时,我的 table 看起来像这样。
自然我希望 "TestData" 位于 "row Headers" 和列 Header 之间, "TestRowNames" 仅在第一列中,然后我也有"TestColNames" 甚至没有出现(如果我没记错的话我需要通过 Jtable 本身而不是 Table 模型更改列 Header)。 =14=]
显然我不理解这里的某些东西,我一直在用头敲键盘一段时间,现在试图弄清楚这个问题。如果你们知道发生了什么事或有任何建议,我会洗耳恭听。
TableModel#getValueAt
应该是 int row, int col
而不是 int col, int row
...
public Object getValueAt(int row, int col) {
您必须删除 "out of bounds" 检查才能使其完全正常工作...因为有超过 5 行
仔细查看 How to Use Scroll Panes 了解如何实现您自己的行 header 而无需伪造它...