tags:

views:

714

answers:

2

I have a class which extends DefaultTableCellRenderer, which renders strings in a monospace font, with a particular color. By default, it appears that tabs are not rendered at all (0 spaces). How can I set the tab size and/or cause them to be rendered?

edits: By "tabs" i mean tab characters, which I would just like to be rendered as some number of spaces. rewriting the string is an option, but I figured there was a better way.

A: 

Half baked answer: replace them with space characters, the quantity you need (need some computing if used beyond just indentation of lines).

PhiLho
+1  A: 

DefaultTableCellRenderer is a JLabel and does what a JLabel will do. I suggest you make yourself a custom TableCellRenderer and return a JPanel with two or more JLabels laid out appropriately. Alternatively a JComponent that override paintComponent to draw as you wish.

Note: attempting to use HTML in renderers is a really bad idea performance wise.

Tom Hawtin - tackline
Exactly why and when is using HTML a bad idea, performance wise? Presumably you have some hard and fast rules?
oxbow_lakes
Just to clarify; we have JTables with >100,000 rows and renderers which make heavy use of HTML, tables, fonts, colors and all.
oxbow_lakes
Each time getTableCellRenderer is called, HTML is set on the JLabel, parsed and all the behind the scenes stuff is done. That tends to be really slow.
Tom Hawtin - tackline
does DefaultTableCellRenderer actually do HTML-like stuff, a la JTextPane? When I said font and color, I just meant that I call setFont() and setBackground() to get the monospace font and appropriate background color.
DLS
Yeah, just start the label text off with <html>, as with other JLabels.
Tom Hawtin - tackline