Passing different object types to a TableModel

2

I'm starting to mess with JTable but I have a question. I can see this post here in another one forum and my doubt is. In case it passes to the model of the table a list of type book:

private List<Livro> valores;         

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

But in my example, I have several entities (not to using book), student, teacher, discipline etc ... I will 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?

    
asked by anonymous 22.07.2014 / 05:07

2 answers

1

The BeanTableModel project exists for this purpose. If you need a little more advanced features I recommend Glazed Lists (this in addition to having a% and have the elements synchronized between the list and the UI ; also have 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);
    
24.07.2014 / 16:57
0

Since you have different lists, you'll have to create a 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 can not share the same methods in TableModel .

    
24.07.2014 / 16:38