[英]How do I make JScrollPane work properly with nested JTables?
The problem I have is that when more rows are added to JTable (jtbls) the vertical scrollbar doesn't appear on my JScrollPane (outer). 我遇到的问题是当更多行添加到JTable(jtbls)时,垂直滚动条不会出现在我的JScrollPane(外部)上。
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 4));
JScrollPane outer = new JScrollPane(panel);
jtbls = new JTable[4];
for (int i = 0; i < jtbls.length; i++) {
jtbls[i] = new JTable(new MyTableModel());
jtbls[i].setFillsViewportHeight(true);
jtbls[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
JPanel inner = new JPanel(new BorderLayout());
inner.add(jtbls[i], BorderLayout.CENTER);
inner.add(jtbls[i].getTableHeader(), BorderLayout.NORTH);
inner.setPreferredSize(new Dimension(outer.getWidth() / 4, 70));
panel.add(inner);
}
add(outer);
You can set all but one of the scroll bars to VERTICAL_SCROLLBAR_NEVER
and set one to VERTICAL_SCROLLBAR_ALWAYS
. 您可以将除一个滚动条之外的所有滚动条设置为
VERTICAL_SCROLLBAR_NEVER
,并将其中一个设置为VERTICAL_SCROLLBAR_ALWAYS
。 Then tie the scroll bar models together like @camickr showed me here . 然后将滚动条模型绑在一起像@camickr 在这里看到的那样 。
This is because you set 这是因为你设定了
inner.setPreferredSize(new Dimension(outer.getWidth() / 4, 70));
So inner
JPanel
is "fine" with height above 70
and don't requests more space to the parent - JPanel panel
which won't request more space to its parent - JVieport
. 所以
inner
JPanel
“很好”,高度超过70
并且不向父级请求更多空间 - JPanel panel
不会向其父级请求更多空间 - JVieport
。
Also if you look at the source code of JTable
- method setFillsViewportHeight
is only applicable to the javax.swing.JLayer
, javax.swing.ScrollPaneLayout
, javax.swing.ViewportLayout
(look at invocations of getScrollableTracksViewportHeight()
) - so this will work only when JTable is inside some viewport. 另外,如果你看一下
JTable
的源代码 - 方法setFillsViewportHeight
只适用于javax.swing.JLayer
, javax.swing.ScrollPaneLayout
, javax.swing.ViewportLayout
(查看getScrollableTracksViewportHeight()
调用) - 所以这只会起作用当JTable在某个视口内时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.