How to set JScrollPane to show only a sepecific amount of rows

房东的猫 提交于 2019-12-01 06:03:17
kleopatra

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

You null it to make the scrollPane to fall back to using the table's preferredSize:

table.setPreferredScrollableViewportSize(null);

Edit

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.

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); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!