Problem filling in JTABLE with ArrayList

-1

Good evening stack overflow ... I write to you because I have a problem, which, until now I have not been able to solve, I already have 2 days writing and erasing code ... The problem consists of:

I create a small graphical interface, to be more exact, a form in swing, then, the form consists of: Name, ID, Address, gener and hobbies. Then, I collect these data with an ACTIONLISTENER in a JBUTTON, and ok, I collect the data and keep it in the respective ARRAYLIST. Note that, I create an object every time I fill the form, therefore, I can add N number of people ... Well the problem is that, create a class where I create my JTABLE, and I want to pass the data of the arraylist to the table. The code I have is:

    public class TablaPanel extends JPanel{

DefaultTableModel modelotabla;
JTable tabla1;
JScrollPane scroll1;
Object columnas[] = {"NOMBRES","CEDULA","DIRECCION","SEXO (M/F)","HOBBIES"};
Object[] fila = new Object[contador.contadorfilas];

 Persona pnew = new Persona(); //esta es la clase que tiene las variables de persona
 FormularioCapa contador = new FormularioCapa(); //para poder obtener contador de cada vez que se presiona 
                                                                                    //el jbutton

public TablaPanel(){

    modelotabla = new DefaultTableModel(columnas,0); //0 son las filas

    tabla1=new JTable(modelotabla);
    add(tabla1);

    ArrayList<Persona> list = lista.getListaDePersonas();


    for(int i=0;i<contador.contadorlista;i++){
    fila[i]=pnew.getName1();
    fila[i]=pnew.getCedula();
    fila[i]=pnew.getAddress();
    fila[i]=pnew.getGender();
        modelotabla.addRow(fila);
    }
    //CONTADORLISTA es para agregar n Filas, y esa n lo define un contador que va sumando 1, cada vez que 
    //se agrega una nueva persona (o sea, cada vez que se presiona el jbutton)

    scroll1 = new JScrollPane(tabla1);
          add(scroll1);

}

The problem is that, it does NOT show me the text in the cells of the JTABLE. But if you take into account the counter. That is, if I add 5 people, I take into account the 5 rows and add them, but NOT the data of the rows, just add the empty rows without any information ...

It should be noted that I have a System.out.println, which shows me the complete arraylist once the n people are added, and if they are saved (n number of people with their data that are added through the graphical interface). The thing is that, it does not show them (the people added and their information) in the cells of the JTable ...

It's my first time working with JTABLE, I hope you understand. Greetings and happy night

    
asked by TELPRO 06.10.2017 в 05:57
source

1 answer

0

Your table seems to be well defined. The problem that I see is that when inserting the rows, you are inserting the correct number of rows but you are always inserting the data of an empty object Persona . You are not using objects Persona of your ArrayList . For this reason, you see the rows but the content of them is empty.

Try to delete that empty person object you have and change your for loop in the following way:

for(int i=0;i<list.size();i++){
    modelotabla.addRow(new Object[] {list.get(i).getName1(), list.get(i).getCedula(), list.get(i).getAddress(), list.get(i).getGender(), list.get(i).getHobbies()});
}
  

NOTE: You do not need to tell the model at any time how many rows   you have to add implicitly (in your case, with the counter that   you're passing it to for ), since you've already told it before   when giving name to the columns.

    
answered by 06.10.2017 в 20:36