我在 ScrollPane 中有一个 JTable,其中包含来自扩展 DefaultTableModel 的数据。当我将数据加载到表中时,它会显示列。滚动条显示数据存在,但不显示。我一滚动数据就会显示出来。我在模型上尝试了各种 fire() 事件,在 Jtable 上尝试了 .repaint()、.revalidate 等,但无法使其正常工作。显然 CellRenderer 没有被调用。我该如何解决这个问题?
public void setTestcaseModel(TestcaseTableModel testcase_model, List<String> names) {
this.testcaseTableModel = testcase_model;
testcaseTable.setModel(this.testcaseTableModel);
testcaseTable.setRowHeight(40);
TableColumn column = testcaseTable.getColumnModel().getColumn(0);
column.setCellRenderer(new TestcaseCellRenderer(names, this, tgc));
TestcaseCellEditor editor = new TestcaseCellEditor(testcaseTable,this);
column.setCellEditor(editor);
editor.addCellEditorListener(tgc);
this.testcaseTableModel.fireTableDataChanged();
}
测试用例表模型
package gui.table;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.jdom2.Element;
@SuppressWarnings("serial")
public class TestcaseTableModel extends DefaultTableModel {
String[] columnNames = { "Testcase" };
Vector<String[]> row_data = new Vector<String[]>();
public TestcaseTableModel(Element testSuite) {
row_data.clear();
List<Element> testcase_list = testSuite.getChildren("TestCase");
String[] data;
for (int i = 0; i < testcase_list.size(); i++) {
data = new String[4];
data[0] = "";
data[1] = "failed";
data[2] = testcase_list.get(i).getAttributeValue("id");
data[3] = "play";
row_data.add(data);
}
}
@Override
public void setValueAt(Object value, int row, int col) {
// Output gui NULL exception
if (value == null)
return;
row_data.elementAt(row)[col] = value.toString();
fireTableCellUpdated(row, col);
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return row_data == null ? 0 : row_data.size();
}
@Override
public String getValueAt(int row, int col) {
return row_data.elementAt(row)[col];
}
public String getColumnName(int column) {
return columnNames[column];
}
public void clear(){
row_data.clear();
fireTableDataChanged();
}
}
感谢您的任何想法。