MVC デザイン パターンを使用して、GUI プロジェクトを作成しています。このプログラムには、AbstractTableModel を拡張するクラス MyTableModel を作成した JTable コンポーネントが含まれています。以下の MyModelClass でわかるように、 addColumnと removeColumnの 2 つのメソッドがあり、通常はテーブルから列を追加/削除しますが、最終的には、8 つの組み合わせを持つ 3 つの初期列をコンストラクターに追加する必要があります。次に、列の追加/削除を試みます。ここで、プログラムのスクリーンショットを見ることができます。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at mvc.TTableModel.getValueAt(TTableModel.java:79)
at mvc.TTableModel.getColumnClass(TTableModel.java:84)
at javax.swing.JTable.getColumnClass(JTable.java:2697)
at javax.swing.JTable.getCellRenderer(JTable.java:5682)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2113)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
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:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443)
at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91).
コードスニペット
public class TTableModel extends AbstractTableModel implements ThreeInputConstants{
private ArrayList<Object> columnList;
private List<boolean[]> data;
public TTableModel() {
this.columnList = new ArrayList<>();
this.data = new ArrayList<>();
// adds p, q, r
columnList.add(THREE_INPUT_TITLES[0]);
columnList.add(THREE_INPUT_TITLES[1]);
columnList.add(THREE_INPUT_TITLES[2]);
for (int i = 0; i < 8; i++) {
boolean[] tempArray = new boolean[3];
tempArray[0] = P_CONST[i];
tempArray[1] = Q_CONST[i];
tempArray[2] = R_CONST[i];
data.add(i, tempArray);
}
}
public void printListTemp() {
System.out.println("Column list: " + columnList);
System.out.println("Data: " + data);
}
public void addColumn(String header) {
this.columnList.add(header);
this.fireTableStructureChanged();
}
public void removeColumn(int columnIndex) {
if (columnIndex >= 0 && columnIndex < getColumnCount()) {
this.columnList.remove(columnIndex + INPUT_COLUMN_COUNT);
this.fireTableStructureChanged();
}
}
@Override
public String getColumnName(int columnIndex) {
return columnList.get(columnIndex).toString();
}
@Override
public int getColumnCount() {
return columnList.size();
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public void setValueAt(Object value, int row, int column) {
//data[row][column] = value;
data.get(row)[column] = (boolean) value;
this.fireTableCellUpdated(row, column);
}
@Override
public Object getValueAt(int row, int column) {
return data.get(row)[column];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
}
public interface ThreeInputConstants {
public static final Object THREE_INPUT_TITLES[] = {
"p", "q", "r"
};
public static final boolean P_CONST[] = {
true, true, true, true,
false, false, false, false
};
public static final boolean Q_CONST[] = {
true, true, false, false,
true, true, false, false,
};
public static final boolean R_CONST[] = {
true, false, true, false,
true, false, true, false,
};
}
更新 (修正済み):
public void addColumn(String header) {
this.columnList.add(header);
for (int i = 0; i < data.size(); i++) {
List<Boolean> rowList = data.get(i);
while (rowList.size() < columnList.size()) {
rowList.add(false);
}
}
this.fireTableStructureChanged();
}