JTable中的错误:java.lang.ArrayIndexOutOfBoundsException:7 >= 7

huangapple go评论73阅读模式
英文:

Error in JTable java.lang.ArrayIndexOutOfBoundsException: 7 >= 7

问题

我在与连接到数据库的 JTable 遇到了问题。只要我添加、删除或修改任何数据,表格就会正确变化。然而,在使用表格的筛选搜索器之后,当我尝试添加内容时,表格直到我进行另一个搜索之后才显示新内容。只有更新按钮有效。

将数据库添加到 JTable 并在进行更改时更新的代码:

public void show_database(){
    try {
           Class.forName("com.mysql.cj.jdbc.Driver");
           cn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/modificar?zeroDateTimeBehavior=CONVERT_TO_NULL", "root", "");
           pst = (PreparedStatement)cn.prepareStatement("SELECT * FROM person");
           rs = pst.executeQuery();
           table.setModel(DbUtils.resultSetToTableModel(rs));
           
    } catch (Exception e) {
        System.out.println(e);
    }
}

筛选器的代码:

DefaultTableModel dtm = (DefaultTableModel)table.getModel();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(dtm);
table.setRowSorter(tr);
int column = 0;
if (combo.getSelectedItem() == "ID") {
    column = 0;
} else if (combo.getSelectedItem() == "Name") {
    column = 1;
} else if (combo.getSelectedItem() == "Surname") {
    column = 2;
} else if (combo.getSelectedItem() == "Adress") {
    column = 3;
}
tr.setRowFilter(RowFilter.regexFilter(filtertxt.getText(), column));

删除的代码:

try {
    pst = (PreparedStatement)cn.prepareStatement("DELETE FROM person WHERE id = ?");
    pst.setInt(1, Integer.valueOf(idtxt.getText().toLowerCase()));
    pst.executeUpdate();
    idtxt.setText("");
    JOptionPane.showMessageDialog(null, "DELETED");
} catch (Exception e) {
}
show_database();

此外,当我尝试删除时,会出现这个错误。而且,我删除的数据会在我再次使用筛选器时显示出来。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8 >= 8
    at java.base/java.util.Vector.elementAt(Vector.java:463)
    ...
英文:

I'm having a trouble with my JTable connected to Database. As soon as i add, delete or modify any data, the table changes correctly. However, after using the the Table's filter searcher, when i try to add something, the table doesn't show it until i make another search. The update button is the only working.

Code to add the database to the JTable and update it when i make a change:

public void show_database(){
        try {
               Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
               cn = (Connection)DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/modificar?zeroDateTimeBehavior=CONVERT_TO_NULL&quot;, &quot;root&quot;, &quot;&quot;);
               pst = (PreparedStatement)cn.prepareStatement(&quot;SELECT * FROM person&quot;);
               rs = pst.executeQuery();
               table.setModel(DbUtils.resultSetToTableModel(rs));
               
        } catch (Exception e) {
            System.out.println(e);
        }
        
    }

The filter's code:

DefaultTableModel dtm = (DefaultTableModel)table.getModel();
 TableRowSorter&lt;DefaultTableModel&gt; tr = new TableRowSorter&lt;DefaultTableModel&gt;(dtm);
 table.setRowSorter(tr);
 int column=0;
        if (combo.getSelectedItem()==&quot;ID&quot;){
            column = 0;
        }
        else if(combo.getSelectedItem()==&quot;Name&quot;){
            column = 1;
        }
        else if(combo.getSelectedItem()==&quot;Surname&quot;){
            column = 2;
        }
        else if(combo.getSelectedItem()==&quot;Adress&quot;){
            column = 3;
        }
        tr.setRowFilter(RowFilter.regexFilter(filtertxt.getText(), column));

Code to delete:

try {
            pst = (PreparedStatement)cn.prepareStatement(&quot;DELETE FROM person WHERE id=(?)&quot;);
            pst.setInt(1, Integer.valueOf(idtxt.getText().toLowerCase()));
            pst.executeUpdate();
            idtxt.setText(&quot;&quot;);
            JOptionPane.showMessageDialog(null, &quot;DELETED&quot;);
        } catch (Exception e) {
        }
        show_database();

Moreover, when i try to delete, it gives me this error. Also, the data i deleted is shown once i use the filter again.:

Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.ArrayIndexOutOfBoundsException: 8 &gt;= 8
	at java.base/java.util.Vector.elementAt(Vector.java:463)
	at java.desktop/javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:660)
	at java.desktop/javax.swing.JTable.getValueAt(JTable.java:2706)
	at java.desktop/javax.swing.JTable.prepareRenderer(JTable.java:5724)
	at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2190)
	at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2092)
	at java.desktop/javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1888)
	at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
	at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
	at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
	at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
	at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
	at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
	at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
	at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
	at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
	at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
	at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

答案1

得分: 0

好的,这是你要的翻译:

好的,我解决了,只需将以下内容添加到筛选器的代码中:

if (filter.getText().equals("")) {
    table.setRowSorter(null);
} else {
    tr.setRowFilter(RowFilter.regexFilter(filter.getText(), column));
}
英文:

Well, i solve it just adding this to the filter's code:

if (filter.getText().equals(&quot;&quot;)){
           table.setRowSorter(null);
       }
       else{
           tr.setRowFilter(RowFilter.regexFilter(filter.getText(), column));
       }


</details>



huangapple
  • 本文由 发表于 2020年10月5日 08:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定