How do I get a JTable header to display entire column names instead of shortening them?

吃可爱长大的小学妹 提交于 2019-12-30 18:54:27

问题


I've a JTable that I fill with a class that extends AbstractTableModel. The problem is that the columns are too short to display the column name. I've turned off Jtable AutoResizeMode property, but shortening still occurs. How do I solve this?


回答1:


Setting off autoresizemode only prevent the JTable's coumns to be resized. To force a particular width retrieve the table column model:

TableColumn column = jtable.getColumnModel().getColumn(col_num);

and set the desired width:

column.setPreferredWidth(width);



回答2:


You can use the Table Column Adjuster to adjust the column widths based on the header text.

The getColumnHeaderWidth(...) shows how to calculate the actual width using the header text and renderer.




回答3:


there are two ways

1) calculate PreferredSize from TableColumn (by defalut returns JLabel), there are 3-4 correct ways how to determine displayed TextLenght in pixex (please add plus 5-8 pixes moreover)

2) setPreferredSize directly, e.g. TableColumn#setPreferredSize



来源:https://stackoverflow.com/questions/7258943/how-do-i-get-a-jtable-header-to-display-entire-column-names-instead-of-shortenin

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