• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TableModel using a HashMap<Integer, Room>

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just 2 business methods: one returning a List, the other one returning nothing
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic