Forums Register Login

color alternate rows/TableModel

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
Except for the wrong capitalization in the class name ( ) that should work just fine.
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6973 times.
Similar Threads
fill color in alternating rows in jtable
How to change the colour of an JTable Cell dynamically
JTable - can different rows be set to different colors?
Hiding text in JTable
Jtable retreiving data
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:01:56.