JTable删除行给出了ArrayOutofBounds异常:2> = 2

时间:2014-12-02 05:15:30

标签: java swing jtable

    public static void main(String[] args)
 {
        // TODO Auto-generated method stub

        Object[][] tdata ={new Object[]{"1","b","e","f"},new Object[]{"2","*","3","4"},new Object[]{"3","@","#","$"}};
        Object[] tname = {"#1","#2","#3","#4"};
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.setDataVector(tdata, tname);    
        JTable jta = new JTable(dtm);
        jta.setRowSelectionAllowed(false);
        jta.getColumnModel().getColumn(0).setCellEditor(new MButtonEditor());
        JFrame jfr = new JFrame();
        jfr.setSize(800, 800);
        jfr.setLayout(new FlowLayout());
        jfr.add(new JScrollPane(jta));
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfr.setVisible(true);
    }

class MButtonEditor extends DefaultCellEditor{

    private int cur_row;
    private int cur_col;
    private JTable cur_tab;
    private JButton jbut;
    MActionListener mactl;
    public MButtonEditor() 
    {
        super(new JTextField());
        this.setClickCountToStart(1); 
        initButton();

    }
    private void initButton()
    {
        jbut = new JButton();

    }

    @Override  
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        cur_row = row;
        cur_col = column;
        cur_tab = table;
        mactl = new MActionListener(this.cur_row,this.cur_col,this.cur_tab);
        jbut.addActionListener(mactl);
        this.jbut.setText(value == null ? "" : String.valueOf(value));

        return jbut;
    }
    @Override  
    public Object getCellEditorValue()  
    {  
        return this.jbut.getText();  
    } 


}
class MActionListener implements ActionListener
{

    private int cur_row;
    private int cur_col;
    private JTable cur_tab;
    DefaultTableCellRenderer backGroundColor;

    MActionListener(int row,int column,JTable table)
    {
        cur_row = row;
        cur_col = column;
        cur_tab = table;
        backGroundColor = new DefaultTableCellRenderer();
        backGroundColor.setBackground(Color.red);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        cur_tab.getColumnModel().getColumn(1).setCellRenderer(backGroundColor);
        DefaultTableModel dtm = (DefaultTableModel) cur_tab.getModel();
        dtm.removeRow(cur_row);
        System.out.println(dtm.getColumnCount());

    }
}

堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:665)
    at javax.swing.JTable.setValueAt(JTable.java:2741)
    at javax.swing.JTable.editingStopped(JTable.java:4723)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141)
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368)
    at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:233)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1010)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6513)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    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)

0 个答案:

没有答案