Load 2 ArrayList in a JTable

0

I have this method that theoretically loads two ArrayList into a JTable

public void CargarJTbable() {
            DefaultTableModel modelo = new DefaultTableModel();
            modelo.addColumn("ID");
            modelo.addColumn("RUT");
            modelo.addColumn("NOMBRE");
            modelo.addColumn("APELLIDO");
            modelo.addColumn("TELEFONO");
            modelo.addColumn("TALLER");

            for (PersonaDTO pers : p.leerPersonaTodos()) {
                Object[] fila = new Object[5];
                fila[0] = pers.getId();
                fila[1] = pers.getRut();
                fila[2] = pers.getNombre();
                fila[3] = pers.getApellido();
                fila[4] = pers.getTelefono();

                modelo.addRow(fila);
            }

            for (TallerDTO tall : t.leerTallerTodos()){
                Object[] fila = new Object[1];
                fila[0] = tall.getNombre();

                modelo.addRow(fila);
            }

            tbl_datos.setModel(modelo);

        }

In theory the last 3 data should be inserted in "Workshop"

Could you help me find the logic to put the data from the list "t.leerTallerAll ()" and add them to "Workshop EDIT: I tried to load it in field 5 but it throws me an error, attached img.

EDIT 2: Now that I changed the code

Object[] fila = new Object[6];
fila[5] = tall.getNombre();

I'm loaded into "Workshop" but below, I should start from the top.

It should be like this. The last field is empty because that person is not associated with a workshop. I edited it manually but I can not do it automatically from the code: (

    
asked by Jorge Martini Schwenke 27.06.2018 в 12:53
source

1 answer

0

The problem is that you are adding new rows with the addRow method. By having two for separated, that is why the rows are added below and not as a field at the end of each record. I suggest the following:

List<TallerDTO> tallerDTOS = t.leerTallerTodos();
int posicionColeccionTaller = 0;
for (PersonaDTO pers : p.leerPersonaTodos()) {
  Object[] fila = new Object[6];
  fila[0] = pers.getId();
  fila[1] = pers.getRut();
  fila[2] = pers.getNombre();
  fila[3] = pers.getApellido();
  fila[4] = pers.getTelefono();
  if (posicionColeccionTaller < tallerDTOS.size()) {
    fila[5] = tallerDTOS.get(posicionColeccionTaller).getNombre();
    posicionColeccionTaller++;
  }
  modelo.addRow(fila);
}

NOTE: I am assuming that a list is returned when executing readTallerAll ()

To have a better and even more specific answer, I would need to know the structures a bit more.

Greetings and I hope it helps you.

    
answered by 27.06.2018 в 14:41