Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JTable“中删除行”;IndexOutOfBoundsException“;_Java_Swing_Indexoutofboundsexception - Fatal编程技术网

Java 从JTable“中删除行”;IndexOutOfBoundsException“;

Java 从JTable“中删除行”;IndexOutOfBoundsException“;,java,swing,indexoutofboundsexception,Java,Swing,Indexoutofboundsexception,我有一个带有自定义表格模型的JTable。这就是我的表格模型的结构 public class defaultTableModel extends DefaultTableModel{ public defaultTableModel(){} public defaultTableModel(String[] ColName,int n){ super(ColName, n); } public vo

我有一个带有自定义表格模型的JTable。这就是我的表格模型的结构

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

        }
    }

}
但我有一个错误:

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)
错误在这个方法上

public void removeAllRow(){
        //setRowCount(0);
        System.out.println(getRowCount());
        while(getRowCount() > 0)
        {
            removeRow(0); //ROW WITH ERROR
        }
    }
我如何解决我的问题

编辑

如果我尝试注释行代码:

table.setRowSorter(sorter);

代码正常运行

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

我很震惊这实际上通过了编译器,但除此之外,您可以通过如下方式清除JTable:

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

希望有帮助。

好吧。。。我没有看到您使用了
setRowCount
,这是有注释的。为什么要逐行删除,而不是使用该方法?我使用了您的代码,但错误是相同的。问题可能来自我的DefaultRowserter???我没有水晶球,为什么不也发布你的DefaultRowserter?为了更快地获得更好的帮助,请发布or。
public void removeAllRow(){
    //setRowCount(0);
    System.out.println(getRowCount());
         this.setRowCount(0);
}