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)) {
这个地方报的

解决方案 »

  1.   

    4,去掉代码中的,默认行选表格事件,后不报异常。加上则报异常,但异常是在这个事件之前报的。 tblBillInfo.addFocusListener(new FocusListener() {
    public void focusGained(FocusEvent e) {
    int row = tblBillInfo.getSelectedRow();
    tblBillInfo.editCellAt(row, 3);
    } public void focusLost(FocusEvent e) {
    }
    });大体情况就是如上所描述,请网上的高手指点一下,谢谢了。
      

  2.   

    用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);
    }