简体   繁体   English

根据其内容垂直调整Jtable的大小

[英]Resize a Jtable Vertically based on it's content

I have a view in my java project that needs to resize a table. 我的Java项目中有一个需要调整表大小的视图。 I have n number of Jtables inside a JScrollPane . 我在JScrollPane中nJtable Some of them doesn't have enough rows to fill the total size of the table. 其中一些没有足够的行来填充表的总大小。 The best solution I think would be to resize the table to fill to the number of rows that were allocated. 我认为最好的解决方案是调整表的大小以填充分配的行数。

1) The table has a dimension of 4x4 but if there is not enough rows (ie, less than 4) it will have a gap, which I don't want. 1)表的尺寸为4x4,但如果没有足够的行(即,少于4行),则会有间隙,这是我不希望的。

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, like having 1, 2 or only 3 rows and not have this gap? 由于此表是静态的并且不会被更改,我如何基于表将具有的行数以编程方式调整该表的大小,例如具有1、2或仅3行且没有此间隙?

PS: I know how many rows it will have, I just don't know how to resize the table height programmatically during runtime. PS:我知道它将有多少行,我只是不知道如何在运行时以编程方式调整表格高度。

表格模型

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, 由于此表是静态的,并且不会更改,因此我该如何根据其具有的行数以编程方式调整该表的大小,

After adding all the data to the table you can use: 将所有数据添加到表后,可以使用:

table.setPreferredScrollableViewportSize(table.getPreferredSize());

Then when the table is added to the scroll pane the size of the scroll pane will be the preferred size of the table. 然后,将表添加到滚动窗格时,滚动窗格的大小将是表的首选大小。

To make the JTable stretch to fit the height of the view, you can call: 要使JTable拉伸以适合视图的高度,可以调用:

table.setFillsViewportHeight(true);

from the javadoc : javadoc

Sets whether or not this table is always made large enough to fill the height of an enclosing viewport. 设置此表是否始终足够大以填充封闭视口的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM