7 Replies - 5115 Views - Last Post: 23 July 2012 - 10:44 AM Rate Topic: -----

#1 davidvee  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 26-June 12

Change Background Color of Jtable Column Heads

Posted 19 July 2012 - 01:05 PM

I have a table with four columns, and I would like each column head to be a different color. I only want the column head to have color, not the rest of the cells in the column. I tried using the DefaultTableCellRenderer, but it made every cell red except for the column heads. What could I change in my code to individually assign a color to each column head?

What I consider the important part is below, and the rest of my code is attached in case that may help.

class CustomRenderer 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, 3, 3);
    c.setBackground(new java.awt.Color(255,72,72));
    return c;
}

}

    table.setDefaultRenderer(Object.class, new CustomRenderer());


Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Change Background Color of Jtable Column Heads

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Change Background Color of Jtable Column Heads

Posted 19 July 2012 - 02:54 PM

A JTableHeader is a Container so

table.getTableHeader().setBackground(color);

shiould work
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Change Background Color of Jtable Column Heads

Posted 19 July 2012 - 03:00 PM

Oups... for different color in different cell you need a CellRenderer

MyRenderer mr = new MyRenderer;
table.getColumnModel().getColumn(0).setCellRenderer(mr);
table.getColumnModel().getColumn(1).setCellRenderer(mr);
table.getColumnModel().getColumn(2).setCellRenderer(mr);

...
class MyRenderer extends CellRenderer {
    Color[] color = {Color.BLUE, Color.WHITE, Color.RED};
    
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              setText(value);
              setBackground(color[column]);
              return this;
         }


This post has been edited by pbl: 19 July 2012 - 03:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 davidvee  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 26-June 12

Re: Change Background Color of Jtable Column Heads

Posted 20 July 2012 - 06:07 AM

Thank you for your help. I'm getting an error on "table.getColumnModel().getColumn(0).setCellRenderer(mr);", specifically under the "getColumnModel()" part. It says Syntax error on token "getColumnModel", Identifier expected after this token.

I'm pretty new to Java, what does that mean exactly?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Change Background Color of Jtable Column Heads

Posted 20 July 2012 - 06:58 AM

Sorry it is table.columnModel() not getColumnModel()

respecting getter the syntax should have been getColumnModel() ...this one slipped under Java Engineering :) it happens some times like the multiple size() instead of getSize()
Was This Post Helpful? 1
  • +
  • -

#6 davidvee  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 26-June 12

Re: Change Background Color of Jtable Column Heads

Posted 20 July 2012 - 07:07 AM

That makes the entire column's cells red/blue/white instead of the column head itself. Is there a way to just change the color of the column head without altering the rest of the cells in the column?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Change Background Color of Jtable Column Heads

Posted 20 July 2012 - 11:18 AM

My fault again... shouldn't answer these in my commuter train

replace setCellRenderer(mr)
by setHeaderRenderer(mr);

Google column header renderer jtable
gave me that link if you want to start from scratch

http://www.examplede...stHeadRend.html
Was This Post Helpful? 1
  • +
  • -

#8 davidvee  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 26-June 12

Re: Change Background Color of Jtable Column Heads

Posted 23 July 2012 - 10:44 AM

View Postpbl, on 20 July 2012 - 11:18 AM, said:

My fault again... shouldn't answer these in my commuter train

replace setCellRenderer(mr)
by setHeaderRenderer(mr);

Google column header renderer jtable
gave me that link if you want to start from scratch

http://www.examplede...stHeadRend.html


That did the trick thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1