跪求解决JTable删除多行后,数据越界问题

kxjh2003 2011-08-07 12:02:33
1,删除勾选行(注我是从行号由大到小删除的,代码中注释的都是我试过方法,可是不行)。
//删除转回操作行
final DefaultTableModel tableModel= (DefaultTableModel) tblBillInfo.getModel();
System.out.println("---------a: "+ tblBillInfo.getRowCount());
for (int i = 0; i < idxs.length; i++) {
// tableModel.fireTableRowsDeleted((int) idxs[i], (int) idxs[i]);
tableModel.removeRow((int) idxs[i]);
// tableModel.getDataVector().remove((int) idxs[i]);
// tableModel.fireTableRowsDeleted((int) idxs[i], (int)
// idxs[i]);
// final int t = (int) idxs[i];
// System.out.println("---------t: "+ t);
// Runnable remover = new Runnable() {
// public void run() {
// tableModel.removeRow(t);
// }
// };
// SwingUtilities.invokeLater(remover);
}
tblBillInfo.revalidate();
// tblBillInfo.repaint();
// tblBillInfo.invalidate();
//tblBillInfo.updateUI();
System.out.println("---------b: " + tblBillInfo.getRowCount());
//SwingUtilities.isEventDispatchThread();
//tblBillInfo.dispatchEvent(e);

2,勾选点击删除后,没有报错,能正常删除,但是当我再去界面,勾选记录时却报错误了
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 4 >= 3
at java.util.Vector.elementAt(Vector.java:432)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:637)
at javax.swing.JTable.setValueAt(JTable.java:1926)
at javax.swing.JTable.editingStopped(JTable.java:3339)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:124)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:334)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:219)
at com.sf.semiautomatic.identify.UI.component.MyCellEditor.stopCellEditing(MyCellEditor.java:57)
at javax.swing.JTable.editCellAt(JTable.java:2667)
at com.sf.semiautomatic.identify.UI.component.JLightEditTable.editCellAt(JLightEditTable.java:47)
at javax.swing.plaf.basic.BasicTableUI$Handler.adjustFocusAndSelection(BasicTableUI.java:955)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:922)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:221)
at java.awt.Component.processMouseEvent(Component.java:5498)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5266)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3968)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

...全文
429 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckystar2008 2013-12-20
  • 打赏
  • 举报
回复
用TableModel的removeRow(index)方法。 要注意一点,调用此方法后是立即生效,即如果有10行,你删除了其中的一行,那么剩下9行。 如果是删除多行,这样写肯定就不对: for(int i=0;i<tableModel.getRowCount();i++) { tabmeModel.removeRow(i); } 这样会报越界。 应该这样写: for(int i=tableModel.getRowCount()-1;i>=0;i--) { tableModel.removeRow(i); }
kxjh2003 2011-08-07
  • 打赏
  • 举报
回复
4,去掉代码中的,默认行选表格事件,后不报异常。加上则报异常,但异常是在这个事件之前报的。

tblBillInfo.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
int row = tblBillInfo.getSelectedRow();
tblBillInfo.editCellAt(row, 3);
}

public void focusLost(FocusEvent e) {
}
});

大体情况就是如上所描述,请网上的高手指点一下,谢谢了。
kxjh2003 2011-08-07
  • 打赏
  • 举报
回复
3,报错地方我为1中tblBillInfo的定义类
package com.sf.semiautomatic.identify.UI.component;

import javax.swing.JTable;
import javax.swing.Action;
import java.util.EventObject;
import javax.swing.SwingUtilities;
import java.awt.Component;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.table.TableCellEditor;

public class JLightEditTable extends JTable {
// actions to be put into editor's actionMap
protected Action rightAction;
protected Action leftAction;
protected Action upAction;
protected Action downAction;
protected Action enterAction;
/* SPACE fast key for SAP 2.0 */
/* author LiuJia */
/* 2006-06-09 */
protected Action spaceAction;
/* end */
protected boolean preMerlin;
protected Action mappingAction = null;

public JLightEditTable() {
super();
initJavaVersion();
}

public JLightEditTable(int row, int col) {
super(row, col);
initJavaVersion();
}

public void setMappingListener(Action act){
mappingAction = act;
}

public boolean editCellAt(int row, int col, EventObject e) {
System.out.println("-------------row: " +row + " col: "+col);
if (super.editCellAt(row, col, e)) {
hackFocusForEditorComponent();
// need to invoke because of new focus architecture
SwingUtilities.invokeLater(new Runnable() {public void run() {
replaceEditorNavigation();
}
});
return true;
}
return false;
}

//---------------------------helper in fact this is where the music plays

/** PRE: isEditing();
*/
protected void hackFocusForEditorComponent() {
// dont care about surrender - always request focus on comp
getEditorComponent().requestFocus();
if (!preMerlin) {
// **** merlin beta 3
// hack around submitted bug (review ID 135159):
// tabbing out of an editing combo focus the next comp
// _outside_ the table
Component c = getEditorComponent();
if (getEditorComponent() instanceof JComboBox) {
JComboBox box = (JComboBox) getEditorComponent();
if (box.isEditable()) {
( (JComponent) box.getEditor().getEditorComponent()).
setNextFocusableComponent(this);
}
} else if (getEditorComponent() instanceof JTextField) {
JTextField txt = (JTextField) getEditorComponent();
txt.selectAll();
}
}
}

/** replaces the left/right arrow key bindings in editorComponent
* with wrappers around table's navigational actions.
*/
protected void replaceEditorNavigation() {
JComponent focused = findFocused();
if (focused != null) {
// replaceEditorNavigation(focused, "RIGHT", "right",
// getRightAction());
// replaceEditorNavigation(focused, "LEFT", "left", getLeftAction());
replaceEditorNavigation(focused, "UP", "up",
getUpAction());
replaceEditorNavigation(focused, "DOWN", "down",
getDownAction());
replaceEditorNavigation(focused, "ENTER", "enter",
getEnterAction());
/* SPACE fast key for SAP 2.0 */
/* author LiuJia */
/* 2006-06-09 */
replaceEditorNavigation(focused,"SPACE","space",
getSpaceAction());
/* end */
}
}

protected void replaceEditorNavigation(JComponent realEditor,
String keyName,
String tableActionName,
Action tableAction) {
KeyStroke navigationKey = KeyStroke.getKeyStroke(keyName);
Object editorActionValue = (String) realEditor.getInputMap().get(
navigationKey);
// nothing registered for the key, so we have to put it in first
if (editorActionValue == null) {
editorActionValue = tableActionName;
realEditor.getInputMap().put(navigationKey, editorActionValue);
}
realEditor.getActionMap().put(editorActionValue, tableAction);
}

protected Action getRightAction() {
if (rightAction == null) {
rightAction = createWrappedAction(this,
getActionMap().get(
"selectNextColumnCell"));
}
return rightAction;
}

protected Action getUpAction() {
if (upAction == null) {
upAction = createWrappedAction(this,
getActionMap().get(
"selectPreviousRowCell"));
}
return upAction;
}

protected Action getDownAction() {
if (downAction == null) {
downAction = createWrappedAction(this,
getActionMap().get(
"selectNextRowCell"));
}
return downAction;
}

protected Action getEnterAction() {
if (enterAction == null) {
enterAction = createWrappedAction(this,
getActionMap().get(
"selectNextRowCell"));
}
return enterAction;
}

/* SPACE fast key for SAP 2.0 */
/* author LiuJia */
/* 2006-06-09 */
protected Action getSpaceAction() {
if (spaceAction == null) {
spaceAction = createWrappedAction(this,
getActionMap().get(
"selectNextRowCell"));
}
return spaceAction;
}
/* end */

protected Action getLeftAction() {
if (leftAction == null) {
leftAction = createWrappedAction(this,
getActionMap().get(
"selectPreviousColumnCell"));
}
return leftAction;
}

protected Action createWrappedAction(final Object source,
final Action action) {
// have to replace the original actionevent with one pointing to
// the table as source
Action wrappedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ActionEvent newEvent = new ActionEvent(source, e.getID(),
e.getActionCommand(), e.getModifiers());

if (mappingAction != null) mappingAction.actionPerformed(newEvent);
action.actionPerformed(newEvent);
}
};
return wrappedAction;
}

/** returns the component that has the focus in editorComponent.
* PRE: isEditing()
*/
protected JComponent findFocused() {
Component result = SwingUtilities.findFocusOwner(getEditorComponent());
if (result instanceof JComponent) {
return (JComponent) result;
}
return null;
}

//---------------------------init ui

protected void initJavaVersion() {
String version = System.getProperty("java.vm.version");
preMerlin = (version == null) || !version.startsWith("1.4");
}

public void refresh(){
Action a = getEnterAction();
if (a != null ){
ActionEvent ae = new ActionEvent(this, 0, "enter");
a.actionPerformed(ae);
}
}

public boolean stopEditing(boolean yes)
{
if(isEditing()) {
int col = getEditingColumn();
int row = getEditingRow();
try {
TableCellEditor ce = getCellEditor( row, col );
if( yes ) {
return ce.stopCellEditing();
} else {
ce.cancelCellEditing();
return true;
}
} catch( Exception e ) {
System.out.println( "Error: " + e.toString() );
return false;
}
}
return true;
}

}


报错的地方为上面代码 if (super.editCellAt(row, col, e)) {
这个地方报的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧