Bolding text in a JTable cell

I was wondering if anybody could please tell me how I can BOLD text in a particular cell within a JTable? From what I've read, it is simply a matter of playing around with a CellRenderer, maybe something like:

void setColour (TableColumn t){
  DefaultTableCellRenderer d = new DefaultTableCellRenderer();
  d.setFont(new Font("Courier", Font.ITALIC, 12));
  //trying to "hard-code"
  Component c = t.getHeaderRenderer().getTableCellRendererComponent(null, t.getHeaderValue(), false, false, 0, 0);
  c.setFont(new Font("Courier", Font.ITALIC, 12));

However, I can't seem to get a visual response from the JTable i.e. by querying the column/cell in question, I can confirm that the Font attribute of the column/cell/renderer has been updated, but there still isn't a change in the table when I run the Frame containing it.

Any suggestions would be greatly appreciated.
Who is Participating?
kvkameshConnect With a Mentor Commented:
 you have forget to make it bold use this one and tell me
 d.setFont(new Font("Courier", Font.ITALIC+Font.BOLD, 12));
this is the exact answer for your question
In the above code you haven't set the property Bold.
Use c.setFont(new Font("Courier", Font.ITALIC + Font.BOLD , 12));
You need to set the font on the renderer.
Or define your own renderer.

((JLabel)t.getHeaderRenderer()).setFont(new Font(("Courier", Font.BOLD, 12));

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

bmarfellAuthor Commented:
Yeah, sorry, I initially had tried BOLD in place of ITALICS, but then I tried ITALICS to confirm that I wasn't getting anywhere.

I've tried all your suggestions but I'm still not seeing either BOLDED or ITALICISED text in my JTable. Is there some property of the JTable or the JScrollPane it sits on that could be over-riding the cell-renderer somehow?

Please read ee guidlines for proposing answers.
That's exactly what tonus said.


Yes, I wasn't sure if setting the font on the renderer would work or not.
Sounds like you'll need to use the other option I specified and install your own renderer:

class MyTableCellRenderer 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);
      c.setFont(new Font("Courier", Font.ITALIC+Font.BOLD, 12));
      return c;

And then use as follows:

   new MyTableCellRenderer());

??? I thought you said you tried it and it didn't work?
And the solution was a copy of tonus's comment?
Mr Objects
sorry boss i have done in my example i have not copied any one's code, please get it right ok.
I know its answer so i couldn't refere any comments. Please don't comment like this please.
Mr tonus  has given,but it is a comment, i have given as a exact answer so Mr bmarfell accepted ok
Thanks for accepting my answer i am working some related application. Retrieving the data from database and displaying in the table. so there i used the same command, when saw your q? i immediately typed the answer.
please ask any q i will give you exact answer not comment?
Thank you
tonus> c.setFont(new Font("Courier", Font.ITALIC + Font.BOLD , 12));

kvkamesh> d.setFont(new Font("Courier", Font.ITALIC+Font.BOLD, 12));

You tell me the difference.

ee guidleines> An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.

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.

All Courses

From novice to tech pro — start learning today.