[英]Remove row from JTable “IndexOutOfBoundsException”
I have a JTable with a custom tableModel. 我有一个带有自定义tableModel的JTable。 So this is the structure of my TableModel
这就是我的TableModel的结构
defaultTableModel 的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 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:
现在,在填充该表之后,我想获取另一个查询以提取记录的子集(例如过滤器),因此我执行了另一个查询,并像下面这样调用方法“ stampatabella”:
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: 我很震惊这实际上通过了编译器,但除此之外,您可以通过将行计数设置为0来清除JTable,如下所示:
public void removeAllRow(){
//setRowCount(0);
System.out.println(getRowCount());
this.setRowCount(0);
}
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.