How to change the font size for strings in a JTable?
Asked Answered
L

3

8
String columnNames[] = {"Time","MAP","ICP","CPP"};
String dataValues[][]= new String [countery]

table = new JTable( dataValues, columnNames );

I am working on a table and storing string values in dataValues. I am curious to know if there is anyway to increase the font size from the default size (which is I assume regular 11 font). This brings my other question...even font color?

Lovering answered 1/6, 2011 at 18:30 Comment(0)
B
31
table.setFont(new Font("Serif", Font.BOLD, 20));
Burnish answered 1/6, 2011 at 19:1 Comment(3)
thanks, now I am trying to increase the size (width) of the table cells. b/c what is happening is the characters are too big for the cell now.Lovering
table.setRowHeight(table.getRowHeight()+gap);Lovering
@Lovering :-) if harcoded Jtables Font(s), then you have to do same with SetRowHeight, or get HEIGHT from FontBurnish
I
5

Try implementing your own Custom Renderer, then you'll be able to treat each string as a JLabel and use setFont(...) accordingly.

Incomprehensive answered 1/6, 2011 at 18:38 Comment(3)
There is no need to create a custom renderer.Lubricate
I did upvote mKorbels answer. I commented (I didn't down vote) on yours because you give misleading information. I don't want the poster to go to all the trouble of creating a custom renderer when the simple solution is one line of code. If you had provided the simple answer and then explained that "for more complex formatting you need to create a custom renderer", I would have upvoted because you would have been first to make the suggestion. I'm glad I'm annoying. I'm tired of people posting misleading or incorrect answers in the hope of getting a few upvotes here and there.Lubricate
Agree, for this question there is no need for customer renderer, but between having font size and rendering issues, this answer was helpful. My need was a combo of a custom renderer and this #24880201 (and sorry for responding to such a long ago post).Piggish
P
2

One option is to set UIManager hints before GUI initialization, e.g.:

FontUIResource font = new FontUIResource("Verdana", Font.PLAIN, 24);
UIManager.put("Table.font", font);
UIManager.put("Table.foreground", Color.RED);
Petroleum answered 1/6, 2011 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.