正确调整JTable的一列标题的大小 [英] Correctly sizing the header for one column of a JTable

查看:193
本文介绍了正确调整JTable的一列标题的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列的表格,希望第一个是所需的最小大小.标题为级别",似乎是25-29像素,具体取决于我使用的字体/操作系统.但是,表格标题似乎至少需要在Windows上为35,而在Mac上至少为52,才能显示不带省略号的内容.我不知道为什么需要额外的空间(没有边框等).

I have a table with 3 columns and want the first one to be the minimum size needed. The header is "Level" and seems to be 25-29 pixels depending on the font/OS I use. However the table header seems to require at least 35 on Windows and 52 on Mac to display it without ellipses. I can't figure out why the extra space is needed (there's no border, etc).

我最终发现以下内容将在Windows(35)上提供所需的大小,但在Mac上它将返回36,这是不够的(很可能是由于排序箭头指示器位于文本的旁边而不是上方). ..

I finally found that the following will give the required size on Windows (35) but on Mac it returns 36 which is not enough (most likely due to the sort arrow indicator being next to, rather than above, the text)...

table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(null, table.getColumnModel().getColumn(0).getHeaderValue(),false,false,0, 0).getPreferredSize().width

table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(null, table.getColumnModel().getColumn(0).getHeaderValue(), false, false, 0, 0).getPreferredSize().width

与平台无关的正确方法是什么?或者,在Mac上是否有一种方法可以计算所需的额外空间,这样我就不必对上面的结果加上15个左右的像素进行硬编码了?

What's the right way to do this that's platform independent? Or, is there a way on Mac to calculate the extra space needed so that I don't have to hardcode something like adding 15 or so pixels to the above result?

推荐答案

我坚持使用

I'd stick with setPreferredScrollableViewportSize() and the approach suggested in Setting and Changing Column Widths, followed by pack().

这篇关于正确调整JTable的一列标题的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆