Using a different background color for odd and even rows is a commonly used trick to improve readability of large tables.
I want to use this effect in Swing’s JTable. I started out by creating a custom table renderer, but this can only be used to paint actual cells, and I also want to add stripes to the “white” part of the table where there might be no cells. I can subclass JTable and override paintComponent(), but I would prefer an option where I can just change the table’s rendering.
Is there a better way of doing this?
Edit: According to the answers so far this seems to be impossible without extending JTable. However, when I override JTable.paintComponent() it also only paints the area where there are rows. How can I paint the rest?
Use
getCellRect( getRowCount() - 1, 0, true ).yto get the top y-coordinate of the empty space, and then paint some Rectangles and (Grid-)Lines withpaintComponent( Graphics g ).To make it much easier for you, here’s a long (but complete) solution 😉
This example could be extended to: