• Post Reply Bookmark Topic Watch Topic
  • New Topic

To use or not to use custom tablemodel?

 
laila NiHai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have the following JTable requirements:
-tablemodel that holds BigDecimal data
-renders BigDecimal data (decimal places may changed dynamically)
-editor that allows revert to previous value if entered data is invalid
-right-click menu option to add/delete a row in the table

I'm torn between using the default tablemodel, renderer, and editor or customized these classes. I've tried a small test program, but the customized classes work for some parts and not others... any thoughts?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A TableModel is used to store data. All your listed requirements affect the table not the model.

a) The DefaultTableModel can hold any Object so it can store a BigDecimal

b) the default Object renderer will simply invoke toString() on the Object. So if you don't like the way the toString() method of the BigDecimal class renders the object then yes you will need to create a custom renderer and add the renderer to the table.

c) the default editors will not update the model when errors are found in the data. The key is to override the getColumnClass(...) method of the JTable or the TableModel to return the proper Class so the appropriate editor can be choosen. The default Number editor may work for you. If not then again you would need to create a custom editor.

d) right click support is done by adding a MouseListener to the table. Then you can invoke the removeRow(...) method of the DefaultTableModel.
 
laila NiHai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Your tips are very clear for a newbie like me.

I learned to override getColumnClass(...) within a customized table model that extends AbstractTabelModel(); without the customized table model, I can't think of how to override the getColumnClass(...). Where does it make sense to put this code?

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you two choices. Override getColumnClass of:

a) JTable
b) DefaultTableModel
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where option b) has the advantage that the column class will be overridden for all tables the model is used in. But that can also be a disadvantage, in which case option a) is best.
 
laila NiHai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I will try both options...if anything will gain more experience.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!