File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes color alternate rows/TableModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "color alternate rows/TableModel" Watch "color alternate rows/TableModel" New topic
Author

color alternate rows/TableModel

darshan sure
Greenhorn

Joined: Aug 25, 2009
Posts: 20
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

Joined: Jun 13, 2009
Posts: 2200
    
    7
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
darshan sure
Greenhorn

Joined: Aug 25, 2009
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: Aug 25, 2009
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

Except for the wrong capitalization in the class name ( ) that should work just fine.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: color alternate rows/TableModel