Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

color alternate rows/TableModel

 
darshan sure
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a TableModel Class that extends AbstractTableModel and here I override methods like getRowCount(),getColumnCount()

and in the main class I have


TableModel model = new TableModel(datafilepath);
JTable table = new JTable();
table.setModel(model);

and now how do I add the following in the TableModel Class as I do not want these code in the main class.



Correct me if am wrong

I am using NetBeans 6.7.1 I tried several things before posting my Question in this forum.

I tried including the following code in the TableModel Class





Any suggestion, hint will help.
Thank you all in advance

 
Rob Camick
Ranch Hand
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prepareRenderer(...) is a method of JTable. So you need to extend JTable to override that method.

The renderering has nothing to do with the TableModel.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right. TableModel, and TreeModel, ListModel etc with it, are non-graphical - they are there for storing the data only. The JTable, JTree, JList etc are what you need to show that data. Check out the Model-View-Controller pattern; the TableModel is the model, the JTable is the view and your code that updates the model is the controller.
 
darshan sure
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply,

So it has to be some thing like




but since am using IDE netbean as table is already instantiated and so I got to do


table.setModel(model);
Component c = table.prepareRenderer(renderer, Index_row, Index_col);

is this the right way of doing else please correct me

Thanks again.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you really have to subclass the JTable for this trick to work, because that method must be called every single time a cell is rendered, not just when you call it manually.
 
darshan sure
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for being patient and making the point clear.

I got it to work.

just to make sure

IDE has a table instantiated already say..
JTable table = new JTable();

now I have this subclass 'shaderow' that extends JTable and has the method that shades the row.
so now I do



hopefully this is ok..
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the wrong capitalization in the class name ( ) that should work just fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic