Change Swing JTable Cell Colors

I am trying to enjoy JTables, TableModels, JTableHeaders, rendering, etc. I am trying to create a simple dummy data table (for practical purposes) that looks like this:

-    1    2   3
A    A1   A2  A3
B    B1   B2  B3
C    C1   C2  C3

I also want cell B2 - and only this cell - to have a blue (Color.BLUE) background - all other cells can have the default color Swing, which they automatically assign.

My code is below and based on the countless examples I have found on this website and on the Internet in general. But I do not get the desired results. Instead, I get a table that looks like this:

A    A1   A2  A3
B    B1   B2  B3
C    C1   C2  C3

Please note that the first line (header) is generally absent. Also, with the code list below, this is done and sets the color of all the color cells, not just the B2 cell that I want.

Code :

public class MyTable
{
    public static void main(String[] args)
    {
        String[][] data = getTableData();
        String[] cols = getTableCols();

        JFrame frame = magicallyCreateJFrame();     // I promise this works!
        MyRenderer myRenderer = new MyRenderer();   // See below

        DefaultTableModel defModel = new DefaultTableModel(data, cols);
        JTable myTable = new JTable(defModel);

        myTable.setDefaultRenderer(Object.class, myRenderer);

        frame.add(myTable);
        frame.pack();
        frame.setVisible(true);            
    }
}

public static String[] getTableCols()
{
    String cols =
    {
        "-",
        "1",
        "2",
        "3",
    };
}

public static String[][] getTableData()
{
    String[][] data =
    {
        {
            "A",
            "A1",
            "A2",
            "A3",
        },
        {
            "B",
            "B1",
            "B2",
            "B3",
        },
        {
            "C",
            "C1",
            "C2",
            "C3",
        },
    };

    return data;
}

And a quick n-dirty MyRendererclass:

public class MyRenderer extends DefaultTableCellRenderer  
{ 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
{ 
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

    if(row == 2 && column == 2)
        c.setBackground(new java.awt.Color(0, 0, 255)); 

    return c; 
} 

} 

Besides being awful code and breaking a lot of “best practices” -types of patterns and methods (remember that this is just what I play with), is there anything that I am doing here that is clearly obvious ? Why am I not getting the table title (first line "- 1 2 3")? Why is my default cell handler not working on the specific B2 cell that I specify?

JTables , . , . , !

+5
5

, reset ( ):

if (! table.isRowSelected(row))
{
    if(row == 2 && column == 2)
        c.setBackground(new java.awt.Color(0, 0, 255));
    else
        c.setBackground(table.getBackground());
}

SSCCE .

+14

Half-:

JTable JScrollPane, . , myTable.getTableHeader(). JScrollPane, .

EDIT:

, , , , , :

if(column == 2 && row == 1) {
    c.setBackground(Color.BLUE); 
} else {
    c.setBackground(Color.WHITE);
}
+4

B2 B2? , if getTableCellRendererComponent, , JTable toString() "B2" B2.

, JScrollPane, JScrollPane .

: Swing JTables, .

+3

, myTable.setDefaultRenderer(String.class, myRenderer); .

0

, - ColorHighlighter. .

0

All Articles