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)
答案 0 :(得分:1)
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 );