如何设置JScrollPane以仅显示特定数量的行 [英] How to set JScrollPane to show only a sepecific amount of rows

查看:609
本文介绍了如何设置JScrollPane以仅显示特定数量的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要一张表来显示3行,这些行可以从DB中获得前3个销售产品。表准备好了,但是除了3行之外,表中有很大一部分几乎是空的。如何设置此滚动窗格的高度以匹配3行的高度?

I only need a table to show 3 rows which get top 3 selling products from DB. The table is ready, but the there's a huge portion of table which is practically empty, except for 3 rows. How can I set height of this scrollpane to match the height of 3 rows?

解决方案:

table.setPreferredScrollableViewportSize(table.getPreferredSize());


推荐答案

这是JTable Scrollable实现的一个怪癖:它设置任意固定的prefScrollable大小。

It's a quirk of the JTable's Scrollable implementation: it sets an arbitrary fixed prefScrollable size.

你将它取消以使scrollPane回退到使用表的preferredSize:


table.setPreferredScrollableViewportSize(null);

编辑

outch,@ edran是正确的,这并不总是在核心layoutManagers的核心表中工作(它是另一个问题的黑客攻击 :)抱歉混乱。在此处复制他的评论:

outch, @vedran is correct, that's not always working in a core table with core layoutManagers (it was a nice hack in another question :) sorry for the confusion. Copying his comment here:

table.setPreferredScrollableViewportSize(table.getPreferredSize());

是一个可行的选择,尽管所有关于硬编码大小的常见提示都适用于此。

is a viable option, though all the usual bewares about hard-coding sizes apply here as well.

表现良好的JTable将实现更合理的getPreferredScrollableViewportSize,并根据内容进行计算。 JXTable(这是表现最好的表,偏向我:-),你可以用可见行设置pref,就像在列表中一样:

A well-behaved JTable would implement a more reasonable getPreferredScrollableViewportSize, f.i calculating it in terms of content. JXTable (which is the best-behaved table, biased me :-), you could set the pref in terms of visible rows, just as in a list:

xtable.setVisualRowCount(3); 

这篇关于如何设置JScrollPane以仅显示特定数量的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆