0

I want to be able to do two things:

  1. set colors of rows based on index, so first row is red, second blue, third green
  2. be able to set color of columns also based on something, be it index or their names etc, whatever is possible.

I do not need to detect selection change or anything. Could someone tell me how to do that? What methods would help etc? In case the title wasn't read, this is regarding DefaultTableModel in JTables.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
Ted
  • 629
  • 2
  • 8
  • 19

1 Answers1

1

set colors of rows based on index,

Table Row Rendering might give you some ideas.

be able to set color of columns also based on something

You can provide a custom render for any column. Then you can add you logic to color the column based on something. Read the JTable API and follow the link to the Swing tutorial on How to Use Table and you will find a section on creating a custom renderer.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I don't have anything exact to check which row in particular to choose, I need first,second and third, but I don't know what they contain. All of the ways there are very useful, but is there one which would allow me to clearly state in setBackground( getBackground() : Color.BLUE); what row I want? – Ted Apr 25 '13 at 16:33
  • 1
    If the condition is just based on row then you can just hardcode the condition based on the current row. – camickr Apr 25 '13 at 17:12