Jtable has a Table Model... create a class that extends Defaulttablemodel, and assemble 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 so:
List<Cliente> listClientes = buscaClientes();
ClienteTableModel model = new ClienteTableModel(listClientes);
table.setModel(model);
You want to send form data to table or database data?
– DiegoAugusto
I couldn’t understand your question. When you refer to "table"/"table", it is unclear whether you speak of the swing Jtable or a table in SQL Server.
– Victor Stafusa
I want to send the form data to the table and after the table to the DB... I use netbeans and sql server 2008
– diiana
Take the data from
TextFields
and move to aList
then load thatList
in your table and at the end of to create a button to save all data from the list in your database.– DiegoAugusto
I’ll try to do that and then put the answer here. Thank you... I’m still learning Java so it’s complicated
– diiana
All right, if you want something more specific just ask here that I try to pass a more complete solution.
– DiegoAugusto
I understand in theory how I have to do it, but can’t you give me an example and explain? Thank you
– diiana
@welcome diiana. You have questions: "How to send data from a form to a
JTable
?" and "How to send data from aJTable
to the database?". A suggestion is that you break this question in two, the way it is at the moment the answers would be broad.– Renan Gomes
Thanks :3 but initially I just really want to figure out how to send the form data to Jtable.. @re22
– diiana