• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableModel using a HashMap<Integer, Room>

 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,

I have just implemented my table model and encouter the following problem. As the underlying data structure, the table model contains a HashMap<Integer, Room>, the integer being the recNo from the data-file and the Room-class containing just the 7 data fields.

Now, my first shot was to implement getValueAt(int rowIndex, int columnIndex) as follows


Obviously, that gives me NullPointerExceptions, because the Map only contains existing Rooms and therefore the record-numbers might have gaps in between them...for example, if the Map contains {(1, room), (2, room), (5, room)} then getRowCount() returns 3 and when Swing calls getValueAt(3, columnIndex) it wont find a room for key=3.

These are the solutions I see:
(1) in the above method, I check if room is null and if so, I just return null --> that results in emtpy rows being displayed in the jtable, so it is not really an option
(2) Discarding the idea of using a map in the table model and use an arrayList instead. i guess, that is the most promising solution, although i dont really love it, because in my entire
business layer i used Map<Integer, Room> and I do not really want to change the Room class....

Of course, i can write another Wrapper, say RoomRecord that contains the recNo and is used exclusively by the TableModel, but IF anybody knows how to use map<Integer, Room>
within the table model, I would be very, very interested....
(3) All i can think of when using Map<Integer, Room> within the model would be to write some fancy stuff within the getValueAt()-method which would somehow check if there is a room
matching the rowIndex and if not returning the room with the next higher index...but i think that overcomplicating things...

I am interested in your ways and ideas...basic question is, what does your table model work on and how do you implement getValueAt()...

THANKS....
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oli,

Frankly I don't think it's a good idea to use a Map to hold the records in the TableModel, simply because a table works index-based and a map does not. In my assignment I used a simple ArrayList to hold the records and my Room-object contained the recNo as well (so I created no seperate wrapper object).

Kind regards,
Roel
 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

thanks...I think, you are right....I changed my tablemodel to work with an ArrayList. I am interested, wether your other business routines also return just an arrayList or do you work with Sets?...

cheers.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just 2 business methods: one returning a List, the other one returning nothing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic