Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Work with Map in JTable?

 
Roman Traze
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently I work with array String [][] to display the data in JTable, but I would like to use instead of a Map<Integer,Room>.

how to work with Map with com JTable and not String [][] ?
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the constructor of my Table model. Maybe this helps: List is an ArrayList, by the way. I use a Value Object for my HotelRooms, and heavily use collections instead of String arrays.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, partner!

Hum... Map<Integer, Room> looks familiar!

Indeed, that certainly works. What you have to do is treat this in the getValueAt method of your table model. Here's what I did:



What I have is an abstract class that extends AbstractTableModel, where I added a few methods (getRecordNumber is one of them, it simply gets the record number of the corresponding row). And I, of course, have the implementation. This is a small part of the implementation. Also, in order for this to work, I disabled the sorting of my table. This way, the records always match the Map of the table model. It is certainly possible to create an algorithm that deals with a table that can be sorted, but since this isn't asked, I did not bother and simply disabled the sorting of my table.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware of those fireXXX methods when working with maps. You need to update map's values after each create/update/delete. Cos when I first play around table model, I forgot to update my map and thought those fireXXX methods didn't work properly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic