How to make all the complete information come out in a JTable

2

I would like to know how to do all the complete information in a JTable that I created in a JScrollPane. I created the header with the DefaultTableModel code. Thanks.

DefaultTableModel mMedico=new DefaultTableModel();
mMedico.addColumn("CÓDIGO");
mMedico.addColumn("NOMBRE");
mMedico.addColumn("APELLIDOS");
mMedico.addColumn("ESPECIALIDAD");
mMedico.addColumn("DNI");
mMedico.addColumn("FECHA DE NAC.");
mMedico.addColumn("EDAD");
mMedico.addColumn("SEXO");
mMedico.addColumn("DIRECCIÓN");
mMedico.addColumn("CORREO");
mMedico.addColumn("TELÉFONO");
mMedico.addColumn("FECHA DE REG.");
mMedico.addColumn("LOGIN");
mMedico.addColumn("CLAVE");
tblMedico.setModel(mMedico);
    
asked by Bruno 19.07.2016 в 20:20
source

3 answers

1

You can build the JTable with the following arguments: Object [] [] data: Two-dimensional arrangement, each element of the array is a row in the table and each element of the row is the value of the respective column. Object [] columns: Tags of the columns. If a String is not used, the value of toString () is taken

    
answered by 20.07.2016 в 01:37
1

As the user @Pedro Martin del Campo said, you can pass both the name of the columns and their respective data in 1 and 2 dimensional arrays respectively as shown in the following code:

String nombresColumnas[] = {"CODIGO", "NOMBRE", "APELLIDOS"};

String datos[][] = {{"1", "roberto", "garcia"}, 
                    {"2", "maria", "perez"}, 
                    {"3","Jorge", "martinez"}};

DefaultTableModel model = new DefaultTableModel(datos, nombresColumnas);
jTable1.setModel(model);        

Clarify that in the example I put a table, jscrollpanel and button that executes the previous code. Greetings ...

    
answered by 21.07.2016 в 02:22
1

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

    
answered by 22.09.2016 в 15:37