さまざまな形で多くの質問が寄せられていることをお詫びしますが、完全に機能する回答をまだ見つけていません。私がやろうとしているのは、テーブルモデルに行を追加することだけです。私はさまざまな方法でそれをやろうとしましたが、常に同様の問題 (または時には新しくてエキサイティングなもの) に遭遇します。とにかく、行を追加しようとすると、「ArrayIndexOutOfBoundsException」エラーが発生します。以下のコードでエラーが発生する行をマークしました。また、私が見たエラーと、私が使用してきたデバッグ出力も含めました。ここに表示されているように DefaultTableModel を拡張する必要がないことは承知していますが、これはバグを追跡するために取り組んできた機能を取り除いたバージョンです。
例外:
スレッド「AWT-EventQueue-0」での例外 java.lang.ArrayIndexOutOfBoundsException: 2 > 0 at java.util.Vector.insertElementAt(Vector.java:594) at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:374) ) javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:350) で javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:361) で SMC.RoleCard$1.actionPerformed(RoleCard.java:69) でjavax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) で javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) で javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) で javax.swing .DefaultButtonModel.setPressed(DefaultButtonModel.java:259) javax.swing.plaf.basic.BasicButtonListener で。mouseReleased(BasicButtonListener.java:252) で java.awt.Component.processMouseEvent(Component.java:6505) で javax.swing.JComponent.processMouseEvent(JComponent.java:3321) で java.awt.Component.processEvent(Component.java) :6270) で java.awt.Container.processEvent(Container.java:2229) で java.awt.Component.dispatchEventImpl(Component.java:4861) で java.awt.Container.dispatchEventImpl(Container.java:2287) で Java で.awt.Component.dispatchEvent(Component.java:4687) で java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) で java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) で java.awt.LightweightDispatcher で。 java.awt.Container.dispatchEventImpl(Container.java:2273) で、java.awt.Window.dispatchEventImpl(Window.java:2719) で java.awt.Component.dispatchEvent(Component.java:4687) で java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) で java.awt.EventQueue.access$200(EventQueue.java:103) で 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) の java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) java.awt.EventQueue$4.run(EventQueue.java:702) の java.awt.EventQueue$4.run( EventQueue.java:700) で java.security.AccessController.doPrivileged(Native Method) で java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) で java.awt.EventQueue で。dispatchEvent(EventQueue.java:699) で java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) で java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) で java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java) :150) java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) で java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) で java.awt.EventDispatchThread.run(EventDispatchThread.java:91) でjava.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) での pumpEvents(EventDispatchThread.java:146) java.awt.EventDispatchThread.run(EventDispatchThread.java:91) での pumpEvents(EventDispatchThread.java:146)java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) での pumpEvents(EventDispatchThread.java:146) java.awt.EventDispatchThread.run(EventDispatchThread.java:91) での pumpEvents(EventDispatchThread.java:146)
デバッグ出力:
There are 2 elements in roleData
roleData has 2 elements
Performing WHILE loop
tableData[0][0] is assigned ECC
tableData[0][1] is assigned 1
Performing WHILE loop
tableData[1][0] is assigned Portal
tableData[1][1] is assigned 2
Called getColumnCount()
Returned 2
tableDate has 2 columns and 2 rows.
Called getColumnCount()
Returned 2
Called getColumnName()
Returned Role
Called getColumnCount()
Returned 2
Called getColumnName()
Returned Sort Order
Called getColumnCount()
Returned 2
arg is :Role Data:
Clicked on Role
Called getColumnClass()
Called getColumnClass()
Called getColumnClass()
Called getColumnClass()
B: There are 2 rows in RoleTableModel
Add was pressed
コード:
package SMC;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* @author Larry Krigbaum
*/
public class RoleCard
{
public JPanel getRoleCard()
{
final JPanel roleCard = new JPanel();
roleCard.setLayout(new BoxLayout(roleCard, BoxLayout.PAGE_AXIS));
roleCard.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel role_title = new JPanel();
role_title.add(new JLabel(" "));
role_title.add(new JLabel("Configure SAP Roles"));
role_title.add(new JLabel(" "));
roleCard.add(role_title);
final JPanel role_table = new JPanel();
String[] roleColumnNames = {"Role", "Sort Order"};
ArrayList<Role> roleData = Role.loadRoleData();
System.out.println("There are " + roleData.size() + " elements in roleData");
//ArrayList<Role> roleData = null;
final RoleTableModel roleTableModel = new RoleTableModel(roleColumnNames, roleData);
JTable roleEditTable = new JTable(roleTableModel);
roleEditTable.setAutoCreateRowSorter(true);
//final DefaultTableModel roleTableModel = (DefaultTableModel) roleEditTable.getModel();
//final JTable roleEditTable = getRoleTable();
role_table.add(new JScrollPane(roleEditTable)); // ScrollPane needed to show headers
roleCard.add(role_table);
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("Add");
addButton.setActionCommand("Add");
JButton deleteButton = new JButton("Delete");
deleteButton.setActionCommand("Delete");
final JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.setEnabled(false);
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(cancelButton);
roleCard.add(buttonPanel);
addButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("B: There are " + roleTableModel.getRowCount() + " rows in RoleTableModel");
System.out.println("Add was pressed");
cancelButton.setEnabled(true);
Object[] newRole = null;
roleTableModel.addRow(newRole);
//Object[] newRole = new Object[] {" ", " "};
System.out.println("A: There are " + roleTableModel.getRowCount() + " rows in RoleTableModel");
//roleTableModel.setRowCount((roleTableModel.getRowCount()) + 1);
System.out.println("B: There are " + roleTableModel.getRowCount() + " rows in RoleTableModel");
roleTableModel.fireTableRowsInserted(roleTableModel.getRowCount(), roleTableModel.getRowCount());
roleTableModel.fireTableDataChanged();
}
});
deleteButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Delete was pressed");
cancelButton.setEnabled(true);
}
});
cancelButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Cancel was pressed");
cancelButton.setEnabled(false);
}
});
return roleCard;
}
class RoleTableModel extends DefaultTableModel
{
private String[] roleColumnNames = {"Role", "Sort Order"};
private Object[][] tableData;
public RoleTableModel(String[] roleColumnNames, ArrayList<Role> roleData)
{
System.out.println("roleData has " + roleData.size() + " elements");
tableData = new Object[roleData.size()][2];
Role role = new Role();
Collections.sort(roleData, Role.MakeComparator);
Iterator rolesOrderByRole = roleData.iterator();
int i = 0;
while (rolesOrderByRole.hasNext())
{
System.out.println("Performing WHILE loop");
role = (Role)rolesOrderByRole.next();
tableData[i][0] = role.xrole;
System.out.println("tableData[" + i + "][0] is assigned " + tableData[i][0]);
tableData[i][1] = role.sortOrder;
System.out.println("tableData[" + i + "][1] is assigned " + tableData[i][1]);
i++;
}
System.out.println("tableDate has " + getColumnCount() + " columns and " + getRowCount() + " rows.");
}
@Override
public int getColumnCount()
{
System.out.println("Called getColumnCount()");
System.out.println("Returned " + roleColumnNames.length);
return roleColumnNames.length;
}
@Override
public int getRowCount()
{
if (tableData == null)
return 0;
return tableData.length;
}
@Override
public String getColumnName(int col)
{
System.out.println("Called getColumnName()");
System.out.println("Returned " + roleColumnNames[col]);
return roleColumnNames[col];
}
@Override
public Object getValueAt(int row, int col)
{
return tableData[row][col];
}
@Override
public Class getColumnClass(int c)
{
System.out.println("Called getColumnClass()");
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
@Override
public boolean isCellEditable(int row, int col)
{
System.out.println("Called isCellEditable()");
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return true;
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
@Override
public void setValueAt(Object value, int row, int col)
{
System.out.println("Called setValueAt()");
tableData[row][col] = value;
fireTableCellUpdated(row, col);
}
}