Insert data into a table from a Java form (Netbeans + SQL Server)

4

I have a form created so that you can enter data on a client and from that form open a button with a button that has the data, so far I have been able to make the button connection. Now my question is how can I send the form data to the table ... I do not want the code, I just need to be told how I do it.

    
asked by anonymous 03.06.2015 / 16:29

1 answer

2

JTable has a Table Model ... create a class that extends DefaultTableModel, and mount your model .. modify the template, then make table.setModel (yourModel)

Example:

for a class with these attributes

class Cliente{
   Integer id;
   String nome;
   //getters and setters
}

You can do the following:

class ClienteTableModel extends DefaultTableModel{
     public ClienteTableModel(){
        this.addColumn("ID");
        this.addColumn("NOME");
     }

     public ClienteTableModel(List<Cliente> listClientes){
        this();
        for(Cliente c: listClientes){
            this.addRow(new String[]{c.getId(),c.getNome()});
        }
     }
}

Then, in your view, you do this:

List<Cliente> listClientes = buscaClientes();
ClienteTableModel model = new ClienteTableModel(listClientes);
table.setModel(model);
    
22.06.2015 / 20:44