Java JTable header word wrap

徘徊边缘 提交于 2019-11-30 15:42:12

You can achieve multi-line headers much easier.

As with many Swing components you can use HTML code. In HTML specify <br> elements to indicate where line breaks / new lines should occur.

For example if you use the following header values (column names):

String[] columnNames = {
    "<html>First<br>column</html>",
    "<html>Second<br>column</html>",
    "<html>Third<br>column</html>"
};

Then the headers will be properly rendered in 2 lines. You don't even need to create/use a custom header renderer, the default header renderer properly handles HTML code.

Note: The header height will be determined by the height of the first column. So you have to use a 2-line HTML value for the first column too. If you only have 1 word for the first column, you may additionally add an empty second line like this: "<html>Select<br>&nbsp;</html>"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!