java 将 JScroll 窗格缩小到与 JTable 相同的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6523974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shrink JScroll Pane to same Height as JTable
提问by Alex Bliskovsky
I currently have JTables nested in JScrollPanes like so:
我目前将 JTables 嵌套在 JScrollPanes 中,如下所示:
My problem is that the number of rows in each table is variable when the table is created. What I want to do is make the JScrollpane smaller if the table is too short, but I want to keep it at a set size if the table is too long.
我的问题是每个表中的行数在创建表时是可变的。如果表格太短,我想要做的是使 JScrollpane 更小,但如果表格太长,我想将其保持在设定的大小。
How can I accomplish this?
我怎样才能做到这一点?
回答by Andrew Thompson
Dimension d = table.getPreferredSize();
scrollPane.setPreferredSize(
new Dimension(d.width,table.getRowHeight()*rows+1));
Where rows
represents the number of rows of data, or the limit.
其中rows
表示数据的行数,或限制。
回答by trashgod
I use setPreferredScrollableViewportSize()
in this context, followed by pack()
or revalidate()
as required.
我setPreferredScrollableViewportSize()
在这种情况下使用,后跟pack()
或revalidate()
根据需要。
Addendum:
附录:
How do you use it?
你如何使用它?
It's a feature of the implementation of Scrollable
in JTable
. Here are several examples. The complementary getPreferredScrollableViewportSize()
may also be useful.
这是Scrollable
in实现的一个特性JTable
。这里有几个例子。补充getPreferredScrollableViewportSize()
也可能有用。
回答by AJNeufeld
The following (currently accepted) code does nottake into account various aspects of the Look and Feel:
以下(当前接受的)代码没有考虑外观的各个方面:
Dimension d = table.getPreferredSize();
scrollPane.setPreferredSize(
new Dimension( d.width, table.getRowHeight() * (rows+1) ));
- The header line is not necessarily
table.getRowHeight()
pixels in height. (In the "Metal" L&F, it is taller) - A vertical scrollbar in the
scrollPane
will squish the table smaller than it's preferred size. - A horizontal scrollbar will take additional space, so not all requested
rows
rows will be visible.
- 标题行的
table.getRowHeight()
高度不一定是像素。(在“金属”L&F 中,它更高) - 中的垂直滚动条
scrollPane
会将表格挤压得小于其首选大小。 - 水平滚动条将占用额外空间,因此并非所有请求的
rows
行都可见。
The following code should be used instead:
应改用以下代码:
table.setPreferredScrollableViewportSize(
new Dimension(
table.getPreferredSize().width,
table.getRowHeight() * visible_rows));
The scrollPane
will ask the table for its preferred size, and adds to that any extra space for the header and scrollbars.
该scrollPane
会要求表的首选大小,并添加到页眉和滚动条任何额外的空间。
Below shows the difference in a JTable
with 4 rows, when visible_rows
is reduced from 4
to 3
. The preferred width of the JScrollPane
is automatically padded to include the scrollbar instead of compacting the table columns. Also note room for the header (which is different from the row height) is also automatically provided; it is not included in the visible_rows
count.
下面显示了 aJTable
与 4 行的差异,当visible_rows
从 减少4
到 时3
。的首选宽度JScrollPane
会自动填充以包含滚动条,而不是压缩表格列。还要注意标题(不同于行高)的空间也是自动提供的;它不包括在visible_rows
计数中。
回答by OscarRyz
Use a GridBagLayout manager and set the maximum size of the JScrollPane to whatever you want to. If the table is larger the JScroll won't grow beyond that size.
使用 GridBagLayout 管理器并将 JScrollPane 的最大大小设置为您想要的任何大小。如果表更大,则 JScroll 不会超过该大小。
Then, add a glue component ( I think it is something like BoxLayout.createVerticalGlue or something like that ) which is an empty component that just takes the remaining space.
然后,添加一个胶水组件(我认为它类似于 BoxLayout.createVerticalGlue 或类似的东西),它是一个只占用剩余空间的空组件。
So, when the table is smaller, this component will take the remaining space, if the table is larger, it won't take anything since the JScrollPane is using it.
因此,当表较小时,此组件将占用剩余空间,如果表较大,则不会占用任何内容,因为 JScrollPane 正在使用它。
回答by WesternGun
If you use MigLayout, when add the scrollpane containing the JTable, you can specify the constraint growx
instead of grow
, to make it stretch only horizontally.
如果您使用 MigLayout,在添加包含 JTable 的滚动窗格时,您可以指定约束growx
而不是grow
, 以使其仅水平拉伸。
Now the scrollpane is located in the middle of its parent. You can set in the row constraint to make it align to top
of the row. Or, in component constraint: aligny top
.
现在滚动窗格位于其父级的中间。您可以在行约束中设置以使其top
与行对齐。或者,在组件约束:aligny top
。