JTable Alternate Row Background 7


Java tables are great but complex. The good thing about them is that you can do with them whatever you want. Even more, the view is completely separated from the model so you can even play more.

JTables in brief (bad thing) are just a bunch of components put together. So anything you can de to a Component you can do in a JTable.

To make a JTable render each row in a different color, you just have to extend the JTable’s prepareRender method.

JTable Alternate Row Color

The above code simply gets the component that was going to be rendered and changes its background so that it fits the user needs. Here we’ve just set the background using two alternate colors, but there are many more things you can do, just use your imagination. Try using getModel(row, column) to get the values of what the cell contains and make the component behave depending on the values it contains.

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on PinterestEmail this to someone

Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “JTable Alternate Row Background