我有一個總是正方形的矩陣,我製作了一個JTable來顯示它的內容。我需要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)
從理論上說,'fireTableStructureChanged'應引起'JTable'到receate /從值從'TableModel'填充'TableModel'。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的響應 – MadProgrammer
您也不應該從模型的上下文外調用'fireTableStructureChanged',爲什麼這些方法是公開的超出我的範圍,您應該要求實現重置自身並觸發事件本身。 – MadProgrammer
@MadProgrammer我已更新我的問題。我仍然嘗試調用fireTableStructureChanged,只是爲了看看發生了什麼,結果是在問題。 – AmiguelS