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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:11:41  来源:igfitidea点击:

Shrink JScroll Pane to same Height as JTable

javaswingjtablejscrollpane

提问by Alex Bliskovsky

I currently have JTables nested in JScrollPanes like so:

我目前将 JTables 嵌套在 JScrollPanes 中,如下所示:

enter image description here

在此处输入图片说明

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 rowsrepresents 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 Scrollablein JTable. Here are several examples. The complementary getPreferredScrollableViewportSize()may also be useful.

这是Scrollablein实现的一个特性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) ));
  1. The header line is not necessarily table.getRowHeight()pixels in height. (In the "Metal" L&F, it is taller)
  2. A vertical scrollbar in the scrollPanewill squish the table smaller than it's preferred size.
  3. A horizontal scrollbar will take additional space, so not all requested rowsrows will be visible.
  1. 标题行的table.getRowHeight()高度不一定是像素。(在“金属”L&F 中,它更高)
  2. 中的垂直滚动条scrollPane会将表格挤压得小于其首选大小。
  3. 水平滚动条将占用额外空间,因此并非所有请求的rows行都可见。

The following code should be used instead:

应改用以下代码:

table.setPreferredScrollableViewportSize(
    new Dimension(
        table.getPreferredSize().width,
        table.getRowHeight() * visible_rows));

The scrollPanewill 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 JTablewith 4 rows, when visible_rowsis reduced from 4to 3. The preferred width of the JScrollPaneis 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_rowscount.

下面显示了 aJTable与 4 行的差异,当visible_rows从 减少4到 时3。的首选宽度JScrollPane会自动填充以包含滚动条,而不是压缩表格列。还要注意标题(不同于行高)的空间也是自动提供的;它不包括在visible_rows计数中。

JTable with and without scrollbar

带和不带滚动条的 JTable

回答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 growxinstead 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 topof the row. Or, in component constraint: aligny top.

现在滚动窗格位于其父级的中间。您可以在行约束中设置以使其top与行对齐。或者,在组件约束:aligny top