简体   繁体   中英

JTable exception remove row

When I remove a row from a Swing JTable then I click on the JTable I get an exception :

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
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)

And this is my code :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn; 

public class TableSelection extends JPanel {

private static JTable tableau = new JTable();
TableColumn sportColumn, sportColumn2;
private JComboBox<String> checkboxs = new JComboBox<String>();
private JComboBox<String> checkboxs2 = new JComboBox<String>();
JScrollPane jsp1, jsp2;
String labels[];

public TableSelection() { 
    labels = new String[11];

    for (int i = 1; i < 10 + 1; i++) {
        labels[i] = ""+(i - 1);
    }

    ((DefaultTableModel) tableau.getModel()).addColumn("Base source");
    ((DefaultTableModel) tableau.getModel()).addColumn("Base destination");

    sportColumn = tableau.getColumnModel().getColumn(0);

    sportColumn2 = tableau.getColumnModel().getColumn(1);

    checkboxs.setModel(new DefaultComboBoxModel(labels));
    checkboxs.setBackground(Color.WHITE);
    checkboxs.setSelectedIndex(0);
    sportColumn.setCellEditor(new DefaultCellEditor(checkboxs));

    checkboxs2.setModel(new DefaultComboBoxModel(labels));
    checkboxs2.setBackground(Color.WHITE);
    checkboxs2.setSelectedIndex(0);
    sportColumn2.setCellEditor(new DefaultCellEditor(checkboxs2));

    ((DefaultTableModel) tableau.getModel()).addRow(new String[] {         labels[0], labels[0] });
    ((DefaultTableModel) tableau.getModel()).addRow(new String[] { labels[0], labels[0] });
    ((DefaultTableModel) tableau.getModel()).addRow(new String[] { labels[0], labels[0] });

    tableau.setPreferredScrollableViewportSize(new Dimension(200, 200));
    tableau.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.add(new JScrollPane(tableau));
    tableau.setRowSelectionInterval(0, 0);

}

public static void main(String[] args) {
    JFrame f = new JFrame("TableSelection");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new TableSelection());

    JButton removeButton = new JButton("remove");
    removeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) { 
            int selected = tableau.getSelectedRow();
            if (tableau.getSelectedRowCount() > 0) {

                ((DefaultTableModel) tableau.getModel()).removeRow(selected);

                if (selected > 1)
                    tableau.setRowSelectionInterval(Math.max(0, (selected - 1)), (Math.max(0, (selected - 1))));

            }
        }

    });

    f.add(removeButton, BorderLayout.SOUTH);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}
}

I already checked the number of lines before and after the delete action and everything seems correct. I think there is something related to the JComboBox components, because when I remove then and use cells with strings only everything works fine. But I really don't see how to fix the problem.

The problem seems to be the fact that a cell of the deleted row was currently in editing mode, and the "stop editing" event fires for a deleted cell. Try to stop the cell edition before the removal part, with :

DefaultCellEditor dce = (DefaultCellEditor)tableau.getCellEditor(); 
if (dce != null) dce.stopCellEditing();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM