• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

JTable color

I am trying to set the grid color of my JTable using

However, this seems to set the color for every grid line, except the leftmost gridline.

Please let me know what I could be missing. Thanks!
  • 5
  • 3
1 Solution
Do you want to change the font color of the selected cells?
what means grid color ?
dshrenikAuthor Commented:
No. I want to change the color of the table border (only the outline - a rectangle).
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

dshrenikAuthor Commented:
Sorry, I used the wrong word - it is not grid color, but I must say "outer border" (rectangle)
dshrenikAuthor Commented:
This is working partially - but does not add a border to the header:
Border tableBorder = BorderFactory.createLineBorder(Color.gray);
            insulineCoverageTable .setBorder(tableBorder);
Maybe then you need to use
This is also example about the borders in the table - it is simpler, but
although shows how to modify the border of
selected cell, not the header , but it gives the general idea

This code is from


The better approach is to override the prepareRenderer(...) method JTable so you can add the code in one place. Here is an example to get you started. In reality you would want to use a CompoundBorder that contains a MatteBorder for the top/bottom and and EmptyBorder for the left/right and you would create a single instance of the Border.

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.border.*;

public class TablePrepareRenderer extends JFrame
    JTable table;

    public TablePrepareRenderer()
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data =
                {"Buy", "IBM", new Double(1000), new Double(80.5), Boolean.TRUE},
                {"Sell", "MicroSoft", new Double(2000), new Double(6.25), Boolean.TRUE},
                {"RSell", "Apple", new Double(3000), new Double(7.35), Boolean.TRUE},
                {"Buy", "Nortel", new Double(4000), new Double(20), Boolean.TRUE}

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable( model )
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class

                public Class getColumnClass(int column)
                        return getValueAt(0, column).getClass();

                public Component prepareRenderer(
                        TableCellRenderer renderer, int row, int column)
                        Component c = super.prepareRenderer(renderer, row, column);
                        JComponent jc = (JComponent)c;

                        //  Color row based on a cell value
                        //  Alternate row color

                        if (!isRowSelected(row))
                                c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
                                jc.setBorder(new MatteBorder(1, 0, 1, 0, Color.RED) );

                        //  Use bold font on selected row

                        return c;

        table.changeSelection(0, 0, false, false);
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

    public static void main(String[] args)
        TablePrepareRenderer frame = new TablePrepareRenderer();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.setLocationRelativeTo( null );

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now