如何强制刷新/重绘JScrollPane? [英] How to force-refresh/repaint a JScrollPane?

查看:139
本文介绍了如何强制刷新/重绘JScrollPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于数据库中的一些东西,我在程序开始时以编程方式将大量组件(JPanels,JLabels等)添加到JScrollPane中。

I am adding lots of components (JPanels, JLabels etc.) into a JScrollPane programagically at the start of my program based on some stuff from a database.

它似乎这个过程对于GUI(?)来说太快了,所以JScrollPane并不总是正确更新,即使内部JPanel大于可见区域,滚动条也不可见。

It seems that this procedure is too fast for the GUI(?), so the JScrollPane does not always update correctly, i.e the scroll bars are not visible even though the inner JPanel is bigger than the visible area.

调整窗口大小(JFrame)可以解决问题,因为我认为Java在重新调整大小时会重新打印组件。

Resizing the Window (JFrame) fixes the problem, as I assume Java is re-printing the components when they are resized.

作为测试,我添加了一个调试按钮,我可以在程序启动完成后单击该按钮。我试图强制JScrollPane刷新自己。

As a test, I have added a debug-button that I can click after the startup of the program has finished. I am trying to force the JScrollPane to "refresh" itself.

我尝试过:

scrollpane.repaint();
scrollpane.validate();
scrollpane.revalidate();

它们似乎都不起作用。但是,如果我更改边框(或任何其他与JScrollPane相关的布局),它会正确刷新。

None of them seems to work. However, if I change the border (or any other layout related to the JScrollPane), it refreshes correctly.

scrollpane.setBorder(new LineBorder(Color.RED));

所以我基本上有2个问题。

So I basically have 2 questions.


  1. 强制滚动窗格刷新的命令是什么?当我添加边框时,显然它正在做某种重绘的事情。我该怎么办?

  1. What is the command for forcing the scrollpane to "refresh"? Obviously it is doing some kind of "repaint" thing when I am adding the border. How can I run that only?

是否有一种方法可以在添加所有组件后暂停组件的打印并再次恢复想要的组件?就像现在一样,我基本上看到屏幕上添加的组件(即使它非常快)。如果我可以添加我想要的所有组件会更好,然后告诉程序将它打印到屏幕/ JFrame。

Is there a way of "pausing" the printing of components as they are added and resume it again after I added all the wanted components? As it is now, I basically "see" the components being added on the screen (even though it is really fast). It would be better if I can add all the components I want and THEN tell the program to print it to the screen/JFrame.


推荐答案

将组件添加到可见面板的基本代码是:

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

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

添加组件无效,因为组件的大小仍为零,因此无需绘制任何内容。当您调用revalidate()方法时,将调用布局管理器,以便组件现在具有位置/大小。然后repaint()将绘制组件。 revalidate()还将导致滚动条在需要时显示。这当然假设您正在使用布局管理器。

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.

这篇关于如何强制刷新/重绘JScrollPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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