• 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

color alternate rows/TableModel

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Shwetha Suresh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Shwetha Suresh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except for the wrong capitalization in the class name ( ) that should work just fine.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic