= 0-6ren"> = 0-有人可以解释一下这个错误吗,我不明白发生了什么?我尝试重置 JTable,这样每次我调用 cargartablaclientes 方法时,它都不会添加更多行。然后加载表格 public void ag-6ren">
gpt4 book ai didi

java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 0 >= 0

转载 作者:行者123 更新时间:2023-12-01 09:07:57 24 4
gpt4 key购买 nike

有人可以解释一下这个错误吗,我不明白发生了什么?我尝试重置 JTable,这样每次我调用 cargartablaclientes 方法时,它都不会添加更多行。然后加载表格

public void agregarnuevafila(javax.swing.JTable Table)
{
//Define temp como una variable de tipo DefaultTableModel
DefaultTableModel temp = (DefaultTableModel) Table.getModel();
Object nuevo[] = {"","","","","","",""};

//Inserta la nueva fila al objeto correspondiente
temp.addRow(nuevo);
}

public void cargartablaclientes(javax.swing.JTable jTable1, String fech)
{
int linea = 1;
reiniciarJTable(jTable1);
try
{
Connection cn = sql.Conectar();

String query = "SELECT * FROM datos where fecha= '"+fech+"' ";
ResultSet rs = sql.stmt.executeQuery(query);
rs.first();
if (rs!=null)
{
do
{
jTable1.setValueAt(rs.getString("serie"), linea, 0);
jTable1.setValueAt(rs.getString("placa"), linea, 1);
jTable1.setValueAt(rs.getString("fecha"), linea, 2);
jTable1.setValueAt(rs.getString("hora_entrada"), linea, 3);
jTable1.setValueAt(rs.getString("tipo"), linea, 4);
jTable1.setValueAt(rs.getString("hora_salida"), linea, 5);
jTable1.setValueAt(rs.getString("total"), linea, 6);
linea++;

if (linea >= 4)
{
agregarnuevafila(jTable1);
}
rs.next();
}while(rs!=null);
}
}
catch(SQLException ex)
{
System.out.println(ex);
}

}

public static void reiniciarJTable(javax.swing.JTable Tabla){
DefaultTableModel modelo = (DefaultTableModel) Tabla.getModel();
while(modelo.getRowCount()>0)modelo.removeRow(0);

TableColumnModel modCol = Tabla.getColumnModel();
while(modCol.getColumnCount()>0)modCol.removeColumn(modCol.getColumn(0));
}

而控制台中的异常显示是这样的

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at sun.swing.SwingUtilities2.convertColumnIndexToModel(SwingUtilities2.java:1905)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2582)
at javax.swing.JTable.setValueAt(JTable.java:2741)
at BaseSQL.modelo.cargartablaclientes(modelo.java:197)
at ParqueoSQL.ParqueoAstor.<init>(ParqueoAstor.java:44)
at ParqueoSQL.ParqueoAstor$17.run(ParqueoAstor.java:1178)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
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)

最佳答案

DefaultTableModel temp = (DefaultTableModel) Table.getModel();
Object nuevo[] = {"","","","","","",""};

//Inserta la nueva fila al objeto correspondiente
temp.addRow(nuevo);

您的 TableModel 有 0 列。您需要在创建 JTable 时指定所需的列数:

DefaultTableModel model = new DefaultTableModel(0, 7);
JTable table = new JTable( model );

关于java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 0 >= 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com