Solved

Bolding text in a JTable cell

Posted on 2001-06-29
9
3,372 Views
Last Modified: 2008-02-01
Hi,
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));
  t.setCellRenderer(d);
 
  //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.
Cheers,
Blair
0
Comment
Question by:bmarfell
9 Comments
 
LVL 1

Expert Comment

by:tonus
ID: 6237584
In the above code you haven't set the property Bold.
Use c.setFont(new Font("Courier", Font.ITALIC + Font.BOLD , 12));
0
 
LVL 92

Expert Comment

by:objects
ID: 6237663
You need to set the font on the renderer.
Or define your own renderer.

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

0
 
LVL 1

Accepted Solution

by:
kvkamesh earned 300 total points
ID: 6237776
HI
 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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:bmarfell
ID: 6239745
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 6239977
kvkamesh,

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

bmarfell,

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:

table.getTableHeader().setDefaultRenderer(
   new MyTableCellRenderer());



0
 
LVL 92

Expert Comment

by:objects
ID: 6243815
??? I thought you said you tried it and it didn't work?
And the solution was a copy of tonus's comment?
:-(
0
 
LVL 1

Expert Comment

by:kvkamesh
ID: 6244439
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
0
 
LVL 1

Expert Comment

by:kvkamesh
ID: 6244460
HI
bmarfell
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
kamesh
0
 
LVL 92

Expert Comment

by:objects
ID: 6247034
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.


0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 20
jdbc error in jsp application 20 45
jboss wildfly 10.1 10 89
How do I remove an object from a 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question