Dislpaying Rows of Jtable with Different colors

hi

my problem is to match rows of two tables table1 and table2

if a row1 of table1 is same as that of row1 in table 2 then background colour of rows should be green

and if they are different then background colour of rows should be red

and if row1 is not present in table2 then its background colour should be yellow

For this

i matched the two rows and set add rows acc in both table and also i am taking a string variable which will have value either "same", "different" , or "new"

and to display different colors

i m using getTableCellRendererComponent

but for this i have to add "same", "different" etc in table

this is a sample code which i was using

{

TableModel jTable1Model = new DefaultTableModel(

new String[][] { { "same", "Two" },

{ "different", "Four" },{ "same", "Four" } },

new String[] { "Column 1", "Column 2" });

jTable1 = new JTable();

jScrollPane1.setViewportView(jTable1);

jTable1.setModel(jTable1Model);

String val = "same";

jTable1.setDefaultRenderer(Object.class, this);

public Component getTableCellRendererComponent(JTable table, Object value, boolean select, boolean focus, int row, int col) {

Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(table, value,select, focus, row, col);

if(value.toString().equals("same")){

renderer.setBackground(Color.green);

}

if(value.toString().equals("different")){

renderer.setBackground(Color.red);

}

if(value.toString().equals("new")){

renderer.setBackground(Color.yellow);

}

return renderer;

}

but the problem in this is that this value should be in a col of a table

then only it give me desired result

but i dont want this value to be displayed in table

so i tried to hide this column by setting width zero etc , nd even removing from columnModel but then also didnt get the result

So please help me out thanks

[2041 byte] By [@34345a] at [2007-11-27 10:48:53]
# 1

if(value.toString().equals("same")){

renderer.setBackground(Color.green);

((JLabel)renderer).setText("");

}

if(value.toString().equals("different")){

renderer.setBackground(Color.red);

((JLabel)renderer).setText("");

}

if(value.toString().equals("new")){

renderer.setBackground(Color.yellow);

((JLabel)renderer).setText("");

}

Regards,

Stas

StanislavLa at 2007-7-29 11:15:39 > top of Java-index,Desktop,Core GUI APIs...
# 2

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

> but the problem in this is that this value should be in a col of a table

No problem, just add the column to the TableModel

> but i dont want this value to be displayed in table

No problem, just remove the TableColumn from the TableColumnModel and it won't be painted in the table view

To do the row coloring you may be able to use my suggestion from this posting:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=613783

camickra at 2007-7-29 11:15:39 > top of Java-index,Desktop,Core GUI APIs...