Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Table Model - Add Column  RSS feed

 
Bill Hayes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a custom table model which extends AbstractTableModel. It is populated with a RowSet. I can't figure out how to write a method to add a column to my custom table model. I understand that when using the DefaultTableModel I can write something like...


Ultimately, what I want to add is a new column as a check box. The user will be able to select any or all rows in the table. The selections will be used to filter a second table.
[ November 13, 2008: Message edited by: Bill Hayes ]
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To display the cell as a checkbox all you need to do is make sure your TableModel's getColumnClass method returns Boolean.class for that column (and of course story only Boolean objects in that column).

Moving to Swing / AWT forum.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the tutorial has an example

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
 
Brian Cole
Author
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Hayes:
I have a custom table model which extends AbstractTableModel. [...] I can't figure out how to write a method to add a column to my custom table model.


There are two things you need to do:

1) Your table model methods need to honor the new column. By that
I mean getColumnCount() needs to return one more than it did before,
getValueAt() needs to return values for that column, etc. [For a row
of checkboxes, getValueAt() should return Boolean.TRUE or Boolean.FALSE
for the new column, and getColumnClass() should return Boolean.class.]

2) Call the fireTableStructureChanged() method inherited from AbstractTableModel.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!