posted 12 years ago
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....