4

enter image description here

I am pretty new to java swing and i just started working on JTable. I want to create a JTable Which look like the above image? Can anybody help me beacause i am not so familiar with JTable?

camickr
  • 321,443
  • 19
  • 166
  • 288
Nikhil
  • 2,857
  • 9
  • 34
  • 58
  • 2
    What about the image do you want to replicate? The header looks like it comes from the XP look and feel. Candy stripping can be achieved by using cell renderers – MadProgrammer Feb 07 '13 at 05:12

2 Answers2

3

Overriding the prepareRender(...) method of the JTable allows you to customize rendering for the entire row without providing custom renderers.

The basic logic would be something like:

JTable table = new JTable( model )
{
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        //  Alternate row color

        if (!isRowSelected(row))
            c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);

        return c;
    }
};

Check out Table Row Rendering for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

How can I achieve the border of the table header like the figure?

You can obtain a copy of the default table header renderer for a given L&F, as shown here, and modify it as desired. With some caveats, you can modify the renderer for a particular TableColumn, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045