Passing Different Object Types to a Tablemodel

Asked

Viewed 505 times

2

I’m starting to mess with Jtable but I’m having a doubt. I’m seeing this post here on another forum and my question is. In case it passes to the table template a list of book type:

private List<Livro> valores;         

public TitulosTableModel(List<Livro> valores) {  
      this.valores = new ArrayList<Livro>(valores);  
}  

But in my example, I have several entities (not using book), student, teacher, discipline etc... I have to make a table model for each entity, or have a way to make the generic table model, to receive a list of any type of object?

  • I just remembered that I had already answered something similar and finally I found: How do I popular a Jtable?. To my surprise you are the author of the other question as well. Wouldn’t that be the case for a duplicate? If it’s not, please explain the difference because now I’m confused.

  • No Math, as popular the table I already know, using Abstracttablemodel, and then in my Tablemodel I get a Student List, what I wanted to know is if I have to create a table model for each type: List of teachers, list of courses... Or if there’s any way I can handle it on the same Tablemodel.

  • Because when I click on students, I want the list of students to appear, when I click on teacher the same thing... But all using the same Tablemodel.

  • Ok, got it! I will remove my answer for now as I am running out of time to update, if I update later I undo the deletion. Good luck.

  • All right! Thanks Math!

2 answers

1


The project Beantablemodel exists for that purpose. If you need Features a little more advanced I recommend the project Glazed Lists (apart from having a TableModel and keep elements synchronized between the list and the UI; also owned facilities to sort and filter data), both are fairly old projects but break a branch.

final EventList<MeuPOJO> eventList = GlazedLists.eventList(minhaListaDePOJOS);
final TableFormat<MeuPOJO> tableFormat = GlazedLists.tableFormat(MeuPOJO.class,
        new String[]{"minhaPropriedade1", "minhaProriedade2"},
        new String[]{"minhaColuna1", "minhaColuna2"});
final AdvancedTableModel<MeuPojo> tableModel = GlazedListsSwing
        .eventTableModel(eventList, tableFormat);  
jTable.setModel(tableModel);

0

Since you have different lists, you will have to create one TableModel for each. Ex: One for List<Livro>, one for List<Professores>, one for List<Alunos> and etc.

Because your beans are different, they will have different fields, so they cannot share the same methods in one TableModel.

  • I did it that way. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.