在Jtable中删除行后出错

时间:2016-02-15 18:59:12

标签: java swing jtable

我的框架包含一个桌子和两个按钮。

enter image description here

1)重复行 2)删除行

表格第三栏中的组合

我有一个场景,我复制空行,然后单击最后一行的组合框以选择该行,然后单击删除行按钮...然后删除每当我尝试单击任何单元格时它给了我跟随错误。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 6
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
    at javax.swing.JTable.setValueAt(Unknown Source)
    at javax.swing.JTable.editingStopped(Unknown Source)
    at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source)
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source)
    at javax.swing.DefaultCellEditor$3.stopCellEditing(Unknown Source)
    at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(Unknown Source)
    at java.awt.AWTEventMulticaster.mousePressed(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$300(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)

以下是相关框架的源代码。

public class UI {

    JFrame frame;
    JTable TblFilePane1;

    UI()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screen = kit.getScreenSize();

        frame = new JFrame("Web Automation Tool (Selenium)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setResizable(false);
        frame.setLocation(screen.width/5, screen.height/300);       
        frame.setPreferredSize(new Dimension(1000, 820));
        frame.pack();
        frame.setVisible(true);

        final JPanel Pane1 = new JPanel(new BorderLayout());

        final JPanel FotterPanelPane1 = new JPanel(new BorderLayout());


        String columnNamesTestSuitePane1[] = { "Scope ID","Test Suite","Data File Required","Data File Path"};
        // Create some data
        String dataValuesTestSuitePane1[][] =
        {
            { "","","Yes","" },
        };      


        // Create a new table instance
        TblFilePane1 = new JTable( dataValuesTestSuitePane1, columnNamesTestSuitePane1 );
        TblFilePane1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        TblFilePane1.getTableHeader().setReorderingAllowed(false);
        TblFilePane1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultTableModel modelPane1 = new DefaultTableModel(dataValuesTestSuitePane1, columnNamesTestSuitePane1);

        TblFilePane1.setModel(modelPane1);
        TblFilePane1.setRowHeight(22);
        final JScrollPane scrollPane1 = new JScrollPane( TblFilePane1 );
        //scrollPane1.setBounds(0, 0, 250, 250);

        //final LineNumberTableRowHeader TblFilePane1LineNumber = new LineNumberTableRowHeader(scrollPane1, TblFilePane1);
        //TblFilePane1LineNumber.setBackground(Color.orange);
        //scrollPane1.setRowHeaderView(TblFilePane1LineNumber);


        TableColumn ScopeIDPane1 = TblFilePane1.getColumnModel().getColumn(0);          
        ScopeIDPane1.setPreferredWidth(30);

        TableColumn TSIDPane1 = TblFilePane1.getColumnModel().getColumn(1);         
        ScopeIDPane1.setPreferredWidth(30);

        TableColumn dfReq = TblFilePane1.getColumnModel().getColumn(2);
        JComboBox cmbDfReq = new JComboBox();
        cmbDfReq.addItem("Yes");
        cmbDfReq.addItem("No"); 
        dfReq.setPreferredWidth(40);

        TableColumn DataFile = TblFilePane1.getColumnModel().getColumn(3);          
        DataFile.setPreferredWidth(490);

        TableColumn NotEditable = TblFilePane1.getColumnModel().getColumn(0);
        JTextField ScopeIDNE = new JTextField();
        ScopeIDNE.setEnabled(false);


        TableColumn NotEditableTS = TblFilePane1.getColumnModel().getColumn(1);
        JTextField TSIDNE = new JTextField();
        TSIDNE.setEnabled(false);

        NotEditableTS.setCellEditor(new DefaultCellEditor(TSIDNE));
        NotEditable.setCellEditor(new DefaultCellEditor(ScopeIDNE));
        dfReq.setCellEditor(new DefaultCellEditor(cmbDfReq));

        Border margin = new EmptyBorder(10, 10, 5, 10);
        FotterPanelPane1.setBorder(margin);

        FotterPanelPane1.add(scrollPane1,BorderLayout.CENTER);

        final JPanel BtnPanelPane1 = new JPanel();
        JButton BtnDuplicateRow = new JButton("Duplicate Row");
        JButton BtnDeleteRowPane1 = new JButton("Delete Row");
        BtnPanelPane1.add(BtnDuplicateRow);
        BtnPanelPane1.add(BtnDeleteRowPane1);

        Pane1.add(FotterPanelPane1,BorderLayout.CENTER);
        Pane1.add(BtnPanelPane1,BorderLayout.SOUTH);

        frame.add(Pane1);

        frame.pack();

        BtnDuplicateRow.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                DefaultTableModel model =  (DefaultTableModel)TblFilePane1.getModel();
                int totalRows = TblFilePane1.getRowCount();

                Vector data = model.getDataVector();

                int[] rows = TblFilePane1.getSelectedRows();
                 if(rows.length != 1)
                 {
                     JOptionPane.showMessageDialog(null, "Please Select one row to Duplicate !");
                 }

                 else
                 {
                     Vector row = (Vector) data.elementAt(rows[0]);
                     row = (Vector) row.clone();                     
                     model.addRow(row);
                 }
            }
        });


        BtnDeleteRowPane1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub                  

                int row = TblFilePane1.getSelectedRow();

                if(row < 0 )
                {
                    JOptionPane.showMessageDialog(null, "Please Select a Row to Delete !");
                    return;
                }
                ((DefaultTableModel) TblFilePane1.getModel()).removeRow(row);

            }
        });

    }


    public static void main(String args[])
    {
        new UI();

    }
    //frame.setMaximumSize(new Dimension(1000,820));
}

1 个答案:

答案 0 :(得分:5)

单击按钮时编辑器仍处于活动状态。您需要在删除行之前停止编辑。

您可以将以下内容添加到ActionListener中:

if (TblFilePane1.isEditing())
    TblFilePane1.getCellEditor().stopCellEditing();

您应该将它添加到两个侦听器。或者,您可能需要查看Table Stop Editing表格中可以设置的属性,以便在表格失去焦点时停止编辑。

此外,变量名称不应以大写字符开头。学习并关注Java Conventions