我有一个总是正方形的矩阵,我制作了一个JTable来显示它的内容。我需要JTable调整其行/列数以匹配矩阵。 我使用的方法调整表格上显示的行数,但列数保持不变。这是为什么?那我该怎么办呢?
以下代码缺少与此问题无关的方法。矩阵正在按原样更新,我查了一下。
public class SomeClass{
TableModel tableModel = new AbstractTableModel() {
public int getColumnCount() {
return matrix.lines.size()+1;
}
public int getRowCount() {
return matrix.lines.size()+1;
}
public Object getValueAt(int row, int col) {
Object val = "+";
if(row == 0 && col == 0)
return " ";
if(row == 0) {
try {
val = matrix.lines.get(0).cells.get(col-1).nodeId;
} catch (Exception e){
val = "-";
}
return val;
}
if(col == 0) {
try {
val = matrix.lines.get(row-1).nodeId;
} catch (Exception e){
val = "-";
}
return val;
}
try {
val = (int)observer.nodeList.get(id).matrix.lines.get(row-1).cells.get(col-1).dist;
} catch (Exception e){
val = "-";
}
return val;
}
};
public void updateTable(){
//STUFF HERE
}
}
更新矩阵后调用updateTable
方法。我已尝试过这种方法的几个实现,结果如下所示。
实施1
public void updateTable(){
((AbstractTableModel) table.table.getModel()).fireTableDataChanged();
}
结果:行数已更新,但列数保持不变。
实施2
public void updateTable(){
((AbstractTableModel) table.table.getModel()).fireTableStructureChanged();
}
结果:大部分时间都可以工作......两个维度都会更新,但偶尔会抛出异常:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2012)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
当您创建自定义TableModel
延长AbstractTableModel
时,您有责任触发正确的TableModel
事件(reference)。
这意味着,当您通过添加/删除列更改matrix
时,您应该调用fireTableStructureChanged
(来自AbstractTableModel
子类)。
如果JTable的autoCreateColumnsFromModel
属性为true,JTable
将丢弃它拥有的任何表列,并按照它们在模型中出现的顺序重新分配默认列。
这意味着您的updateTable方法应为:
public void updateTable()
{
fireTableStructureChanged();
}