我试图在单击按钮后向我的表插入一行,但是我收到了下面的堆栈异常。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at com.testingarea.TestModel$2.actionPerformed(TestModel.java:64)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我已在下方创建了SSCCE,因此您可以重新创建。我不完全确定为什么会发生这种情况,我认为这可能与我的模型有关?
package com.testingarea;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class TestModel {
private DespatchTypeParameterTableModel _model = new DespatchTypeParameterTableModel();
private JTable _table = new JTable(_model);
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestModel().makeUI());
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private Component makeUI() {
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(_table), BorderLayout.NORTH);
p.add(createButtonPanel(), BorderLayout.SOUTH);
return p;
}
private Component createButtonPanel() {
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton insertButton = new JButton("Insert Despatch Type Parameter");
insertButton.addActionListener(new ActionListener() {
@SuppressWarnings("rawtypes")
@Override
public void actionPerformed(ActionEvent event) {
int lastSelectedRow = _table.getSelectionModel().getMaxSelectionIndex();
int realRow = lastSelectedRow;
Vector vector = new Vector();
_table.clearSelection();
if (lastSelectedRow == -1) {
((DefaultTableModel)_model).insertRow(_table.getRowCount(), vector);
_model.addDespatchTypeParameter(new DespatchTypeParameter("Test Name", "Test Description", "Test Value"));
_table.getSelectionModel().setSelectionInterval(_table.getRowCount() - 1,
_table.getRowCount() - 1);
} else {
((DefaultTableModel)_model).insertRow(realRow + 1, vector);
_model.addDespatchTypeParameter(new DespatchTypeParameter("Test Name", "Test Description", "Test Value"));
_table.getSelectionModel().setSelectionInterval(realRow + 1, realRow + 1);
}
}
});
buttonPanel.add(insertButton);
return buttonPanel;
}
@SuppressWarnings("serial")
private static final class DespatchTypeParameterTableModel extends DefaultTableModel {
private Vector<DespatchTypeParameter> _dtParameterList;
public DespatchTypeParameterTableModel() {
setColumnIdentifiers(new String[]{"Parameter Name", "Parameter Description", "Parameter Value"});
_dtParameterList = new Vector<DespatchTypeParameter>();
}
@Override
public int getRowCount() {
return _dtParameterList == null ? 0 : _dtParameterList.size();
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
Object value = null;
DespatchTypeParameter dtParameter = getDespatchTypeParameter(row);
if (getColumnName(column).equals("Parameter Name")){
value = dtParameter.getParameterName();
}else if (getColumnName(column).equals("Parameter Description")){
value = dtParameter.getParameterDescription();
}else if (getColumnName(column).equals("Parameter Value")){
value = dtParameter.getParameterValue();
}
return value;
}
public DespatchTypeParameter getDespatchTypeParameter(int row){
if (row < 0 || row >= _dtParameterList.size()){
return null;
}
return (DespatchTypeParameter)_dtParameterList.get(row);
}
public void addDespatchTypeParameter(DespatchTypeParameter despatchTypeParameter) {
if (despatchTypeParameter != null){
_dtParameterList.add(despatchTypeParameter);
fireTableRowsInserted(_dtParameterList.size() - 1, _dtParameterList.size() - 1);
}
}
@Override
public void removeRow(int row) {
if (row >= 0 && row < _dtParameterList.size()){
_dtParameterList.remove(row);
fireTableRowsDeleted(row, row);
}
}
@Override
public Vector<DespatchTypeParameter> getDataVector() {
return _dtParameterList;
}
}
private static class DespatchTypeParameter {
private String _parameterName;
private String _parameterDescription;
private String _parameterValue;
public DespatchTypeParameter(String parameterName, String parameterDescription, String parameterValue) {
_parameterName = parameterName;
_parameterDescription = parameterDescription;
_parameterValue = parameterValue;
}
public String getParameterName() {
return _parameterName;
}
public String getParameterDescription() {
return _parameterDescription;
}
public String getParameterValue() {
return _parameterValue;
}
}
}
答案 0 :(得分:1)
我似乎已经通过删除
修复了它 @Override
public int getRowCount() {
return _dtParameterList == null ? 0 : _dtParameterList.size();
}