Table headers not centered

I was hoping somebody could help me with some formatting issues I am finding with a table I am using. I have table component that I would like the header to be centered and word wrapped. I added html to the header to center and break the two words into separate lines but the text doesn’t exactly center. it is especially noticeable when resizing the cell. Anybody have a suggestion as to what I can do to get it to center?

Hmmm. That space is reserved for a “sort” indicator arrow, if I recall correctly. You could dig around in Java Swing’s JTable.setTableHeader() method documentation to try to substitute one that centers the way you wish.

To illustrate what Phil is talking about:

Thank you for your replies, I guess I will just have to live with it since I do not have the expertise to mess with the java.

You’ll probably want this to fire automatically - probably on propertyChange of the data attribute, but this should get you started. table.getColumnModel().getColumn(1) sets the index of the column to modify (from 0, counting from left to right).

from javax.swing.table import TableCellRenderer

table = event.source.parent.getComponent('Table').getTable()

class NonSortableHeaderRenderer(TableCellRenderer):
	def getTableCellRendererComponent(self, table, value, selected, focused, row, col):
		renderer = table.getTableHeader().getDefaultRenderer()
		comp = renderer.getTableCellRendererComponent(table, value, selected, focused, row, col)
		comp.setIcon(None)
		return comp
		
table.getColumnModel().getColumn(1).setHeaderRenderer(NonSortableHeaderRenderer())

2 Likes

Thank you very much. I will give this a shot.

To kind of follow up what you had posted here about the centering, could I do something similar to modify the header’s color and line type? I would like it to have a plain line instead of what appears to be an etched or beveled line.

Yes, you can - you would need to modify the comp returned in the middle of the script. For what you can do, unfortunately there's no reference more useful than the Javadocs for JComponent/JLabel:
https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html
https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html

Thank you very much.