我正在尝试对我的JTable扩展AbstractTableModel进行修改。我创建了一个堆来插入所有的文档,然后我在堆数组上应用了一个heapSort,所以这个有序数组应该是我的TableModel数据。看起来是这样的:
public class ModeloTabla extends AbstractTableModel {
private Heap heap;
private Nodo[] datos;
@Override
public int getRowCount() {
return heap.getNumNodos();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if ( !heap.empty() ) {
datos = heap.heapSort();
}
Documento doc = datos[rowIndex].getDocumento();
switch ( columnIndex ) {
case 0:
return doc.getNombre();
case 1:
return doc.getHojas();
case 2:
return doc.getPrioridad();
default:
return null;
}
}
}在getValueAt方法中,当我调用heap.heapSort()时,堆内部数组被销毁,它返回一个具有有序节点的Nodo[]。因此,当datos有一个带节点的有序数组时,我的JTable将不会显示数据。现在,如果我不执行heap.heapSort(),而只是从堆中调用无序数组,那么JTable将显示所有内容。
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
datos = heap.getDatos();
Documento doc = datos[rowIndex].getDocumento();
... //This works but datos is unordered
}
}我尝试用heapSort()内部的有序数组替换Heap无序数组,然后使用getDatos()返回它,但是JTable再次没有出现,我还检查了来自heapSort()的返回数组,它运行良好,数据与来自getDatos()的数据相同,但已排序。对此有任何帮助将不胜感激,谢谢。
发布于 2013-10-31 19:19:39
在getValueAt()方法中,您将从datos对象中检索数据。
Documento = datosrowIndex.getDocumento();
因此,行计数应该基于datos对象中的行数。
public int getRowCount() {
//return heap.getNumNodos();
return datos.length;
}getValueAt()方法不应该对数据进行排序。模型中的数据应该已经排序了。要么在外部排序,要么在创建模型时对其进行排序。也就是说,getValueAt()方法不应该更改数据的结构。而且,每次您更改数据时,都需要求助于此。
https://stackoverflow.com/questions/19714844
复制相似问题