I've searched for my problem for hours, but none of the solutions I tried have solved it. I want to generate around 40 rows in a table with 6 columns for a project. However, when I run the code in a Swing GUI, the rows don't appear in the table. Each row is either a String or a Double, except for the last row, which is going to be a Panel. Here is the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26 > 0
Here is the function with the problem:
public void generateRows() {
setRowCount(0);
int cont = 0;
for (Object obj : productos) {
addRow(obtenerAtributosProducto((ProductoComida) obj));
cont++;
}
setRowCount(cont);
}
private Object[] obtenerAtributosProducto(ProductoComida producto) {
/*
*Los atributos se obtendrán en el siguiente orden: "Id", "Nombre",
* "Precio", "Marca"
* , "Calificación"
* */
rowData[0] = producto.obtenerId().toString();
rowData[1] = producto.obtenerNombre();
rowData[2] = producto.obtenerPrecio();
rowData[3] = producto.obtenerMarca();
rowData[4] = producto.obtenerCalificacion();
rowData[5] = "Here is a panel";
return rowData;
}
This is the rest of the code:
public class ComprarComidaTableModel extends DefaultTableModel {
private static final String[] titles = {
"Id", "Nombre", "Precio", "Marca"
, "Calificación", "Opciones"
};//La última fila es la fila del botón, la cantidad, y obtener más información del producto.
private Inventario inventario = new Inventario();;
private Object[] productos;
private String filtro;
private Object[] rowData = new Object[6];
public ComprarComidaTableModel() {
super(0, 0);
this.productos = inventario.obtenerCatComida().obtenerProductos();
generateColumns();
generateRows(); //This method is the problem
this.filtro = "Ninguno";
}
I know that I don't need to do this for a single table, but I have to do almost 3 tables like this and I think this is the best way to simplify the code.