简体   繁体   中英

Remove row from JTable “IndexOutOfBoundsException”

I have a JTable with a custom tableModel. So this is the structure of my TableModel

defaultTableModel

 public class defaultTableModel extends DefaultTableModel{

        public defaultTableModel(){}

        public defaultTableModel(String[] ColName,int n){
            super(ColName, n); 
        }

        public void setValueAt(Object value,int row,int column){
            super.setValueAt(value, row, column);
        }

        public void removeAllRow(){
            //setRowCount(0);
            System.out.println(getRowCount());
            while(getRowCount() > 0)
            {
                removeRow(0);
            }
        }
    }

MyTableModelArticoliDett

public class MyTableModelArticoliDett extends defaultTableModel{
    public List<Articoli> v;
    public LinkedHashMap<String,Articoli> mappaArticoli = new LinkedHashMap<String,Articoli>();
    public List<Magazzino> listaMagazzino;

    public MyTableModelArticoliDett(String[] ColName,List<Magazzino> listaMagazzino) {
        super(ColName, 0);    // 10 righe iniziali (vuote)
        this.listaMagazzino = listaMagazzino;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Class<Float> getColumnClass(Float columnIndex) {
        return Float.class;    // Le due colonne sono numeri interi
    }

    @SuppressWarnings("rawtypes")
    public void stampaTabella(List<Articoli> v){
        removeAllRow();
        this.setV(v); // inizializzato con il vettore   
        int i=0;
        for(Iterator<Articoli> it = v.iterator(); it.hasNext();){
            Articoli articoli = it.next();
            int numberCol=0;
            mappaArticoli.put(articoli.getCodArticoloString(), articoli);
            this.addRow(new Vector());

        }
    }

}

Now I use this code to insert items in my table:

    List<Articoli> v = modelManager.getArticoliManager().estraiArticoli(false, true,false,true,true,null,false,null);
    tableModel2 = new MyTableModelArticoliDett(listaMagazzino);
    sorter = new TableRowSorter<MyTableModelArticoliDett>(tableModel2);
            tableModel2.stampaTabella(v);
table = new CustomTableArticoliDett(tableModel2);
        table.setRowSorter(sorter);

I can see the items. Now after that I have populate this table, I want to get another query to extract a subset of record (es. filter), so I execute another query, and I call method "stampatabella" like this:

tableModel2.stampaTabella(new array);

But I have this error:

DEBUG [AWT-EventQueue-0] (MyLog4J.java:45) - java.lang.IndexOutOfBoundsException: Invalid range
    at javax.swing.DefaultRowSorter.rowsDeleted(DefaultRowSorter.java:880)
    at javax.swing.JTable.notifySorter(JTable.java:4274)
    at javax.swing.JTable.sortedTableChanged(JTable.java:4118)
    at javax.swing.JTable.tableChanged(JTable.java:4395)
    at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
    at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
    at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:464)
    at com.mcsolution.table.MioTableModel.defaultTableModel.removeAllRow(defaultTableModel.java:38)
    at com.mcsolution.table.MioTableModel.Annaelle.MyTableModelArticoliDett_Annaelle.stampaTabella(MyTableModelArticoliDett_Annaelle.java:30)
    at com.mcsolution.easyMgmt.panel.articoli.articoliDettAbbigliamento.panelArticoliDett_GraficaAbbigliamento.filtraArticoliPerCategoria(panelArticoliDett_GraficaAbbigliamento.java:1320)
    at com.mcsolution.easyMgmt.panel.articoli.articoliDettAbbigliamento.panelArticoliDett_GraficaAbbigliamento$2.actionPerformed(panelArticoliDett_GraficaAbbigliamento.java:822)
    at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:588)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:835)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:499)
    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:4492)
    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)

The error is on this method

public void removeAllRow(){
        //setRowCount(0);
        System.out.println(getRowCount());
        while(getRowCount() > 0)
        {
            removeRow(0); //ROW WITH ERROR
        }
    }

How can I fixed my problems?

EDIT

If I try to comment the line code:

table.setRowSorter(sorter);

the code works

您可以使用setRowCount(0)清除表。

I'm shocked this actually passed the compiler, but that aside, you clear a JTable, by setting the row count to 0 , like the following:

public void removeAllRow(){
    //setRowCount(0);
    System.out.println(getRowCount());
         this.setRowCount(0);
}

Hope that helps.

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