How to force-refresh/repaint a JScrollPane?

社会主义新天地 提交于 2019-12-01 04:32:58

The basic code for adding components to a visible panel is:

panel.add(...);
panel.add(...);
panel.revalidate();
panel.repaint();

Adding a component does nothing because the component still has a zero size so there is nothing to paint. When you invoke the revalidate() method the layout manager gets invoked so components will now have a location/size. The repaint() will then paint the components. The revalidate() will also cause the scrollbars to show when required. This of course assumes you are using layout managers.

The components are added to the panel so you invoke the methods on the panel, not the scrollpane.

In my case only

frame.pack();

helped to get the scrollbars on the JScrollPane, when the enclosed JPanel was resized dynamically.

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