In addition to what they tell you @PedroMartindelCampo and @ReneGarnica, I show you an unconventional way, but that will undoubtedly save you a lot of work when adding items to a table, especially if you bring them from the database .
Requirements
Now we can start.
Create an entity
This entity will represent a table in the database, in this way, its properties are equivalent to the columns in the table. In this case, I have a table Person
with the following columns:
- Name
- Last name
- F_Nac
- DNI
- Address
- Phone
- Email
Which we are going to map an entity:
public class Person {
private String name;
private String lastname;
private Date birthDate;
private String dni;
private String address;
private String phone;
private String email;
public Person() {
}
public Person(String name, String lastname, Date birthDate,
String dni, String address, String phone,
String email) {
this.name = name;
this.lastname = lastname;
this.birthDate = birthDate;
this.dni = dni;
this.address = address;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
If you notice, each property is the equivalent of a column in the table.
Create the bindings for the table
Now it is necessary to create the bindings for the table. We do this with the aforementioned library.
private void initBindings() {
group = new BindingGroup();
JTableBinding binding = SwingBindings.createJTableBinding(
UpdateStrategy.READ,
people, // <- importante
tblPeople
);
group.addBinding(binding);
this.initEntityBindings(binding);
group.bind();
}
As you can see it is not very difficult. We just create bindings for JTable, add it to the group of bindings and apply the bindings by calling BindingGroup#bind
. However, look at the line:
people, // <- importante
The variable people
will be the one that contains the list of people each time a query is made to the database. That is, we will not create a List<Person>
every time we make a query; instead, we will clean the list and fill it in again with the results of the new query. Therefore, it is recommended that the list be a constant somewhere (preferably in the form controller):
private static final List<Person> people;
We also see that we call initEntityBindings
, which we will see below.
Associate bindings with properties
Here what we do is simply associate each column of the binded table with a property of the related entity.
private void initEntityBindings(JTableBinding binding) {
JTableBinding.ColumnBinding colBinding = binding.addColumnBinding(ELProperty.create("${name}"));
colBinding.setColumnName("Nombre");
colBinding.setColumnClass(String.class);
colBinding = binding.addColumnBinding(ELProperty.create("${lastname}"));
colBinding.setColumnName("Apellido");
colBinding.setColumnClass(String.class);
colBinding = binding.addColumnBinding(ELProperty.create("${birthDate}"));
colBinding.setColumnName("F. Nac");
colBinding.setColumnClass(Date.class);
colBinding = binding.addColumnBinding(ELProperty.create("${dni}"));
colBinding.setColumnName("DNI");
colBinding.setColumnClass(String.class);
colBinding = binding.addColumnBinding(ELProperty.create("${address}"));
colBinding.setColumnName("Dirección");
colBinding.setColumnClass(String.class);
colBinding = binding.addColumnBinding(ELProperty.create("${phone}"));
colBinding.setColumnName("Teléfono");
colBinding.setColumnClass(String.class);
colBinding = binding.addColumnBinding(ELProperty.create("${email}"));
colBinding.setColumnName("Correo E.");
colBinding.setColumnClass(String.class);
}
As we can see, for each column of the table, we associate the name of the property of the entity and the type of data. Simple.
Demonstration
To demonstrate the operation you only have to make a query to the database and map the results in your entity.
while(rs.next()) {
Person person = new Person();
person.setName(rs.getString("nombre"));
person.setLastname(rs.getString("apellido"));
person.setBirthDate(rs.getDate("f_nac"));
person.setDni(rs.getString("dni"));
person.setAddress(rs.getString("direccion"));
person.setPhone(rs.getString("telefono"));
person.setEmail(rs.getString("email"));
PersonController.add(person);
}
Where:
public class PersonController {
public static final List<Person> people = new ArrayList<>();
public static void add(Person p) { people.add(p); }
}
Result