根据内容垂直调整 Jtable 的大小
Resize a Jtable Vertically based on it's content
我的 java 项目中有一个视图需要调整 table 的大小。
我在 JScrollPane 中有 n 个 Jtables。其中一些没有足够的行来填充 table 的总大小。我认为最好的解决方案是调整 table 的大小以填充分配的行数。
1) table 的尺寸为 4x4,但如果没有足够的行(即少于 4 行),它将有间隙,这是我不想要的。
因为这个 table 是静态的,不会改变我如何根据它将拥有的行数以编程方式调整这个 table 的大小,比如有 1、2 或只有 3行,没有这个差距?
PS:我知道它会有多少行,我只是不知道如何在运行时以编程方式调整 table 高度。
要使 JTable
拉伸以适合视图的高度,您可以调用:
table.setFillsViewportHeight(true);
来自 javadoc:
Sets whether or not this table is always made large enough to fill the height of an enclosing viewport.
Since this table is static and won't be changed how can I resize this table programmatically based on the number of rows that it will have,
将所有数据添加到 table 后,您可以使用:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
然后当 table 添加到滚动窗格时,滚动窗格的大小将是 table 的首选大小。
我的 java 项目中有一个视图需要调整 table 的大小。
我在 JScrollPane 中有 n 个 Jtables。其中一些没有足够的行来填充 table 的总大小。我认为最好的解决方案是调整 table 的大小以填充分配的行数。
1) table 的尺寸为 4x4,但如果没有足够的行(即少于 4 行),它将有间隙,这是我不想要的。
因为这个 table 是静态的,不会改变我如何根据它将拥有的行数以编程方式调整这个 table 的大小,比如有 1、2 或只有 3行,没有这个差距?
PS:我知道它会有多少行,我只是不知道如何在运行时以编程方式调整 table 高度。
要使 JTable
拉伸以适合视图的高度,您可以调用:
table.setFillsViewportHeight(true);
来自 javadoc:
Sets whether or not this table is always made large enough to fill the height of an enclosing viewport.
Since this table is static and won't be changed how can I resize this table programmatically based on the number of rows that it will have,
将所有数据添加到 table 后,您可以使用:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
然后当 table 添加到滚动窗格时,滚动窗格的大小将是 table 的首选大小。