Java 如何强制刷新/重新绘制 JScrollPane?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22513032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force-refresh/repaint a JScrollPane?
提问by Daniele Testa
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.
我在程序开始时根据数据库中的一些内容以编程方式将许多组件(JPanels、JLabels 等)添加到 JScrollPane 中。
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.
似乎这个过程对于 GUI(?) 来说太快了,所以 JScrollPane 并不总是正确更新,即即使内部 JPanel 大于可见区域,滚动条也不可见。
Resizing the Window (JFrame) fixes the problem, as I assume Java is re-printing the components when they are resized.
调整窗口大小 (JFrame) 解决了这个问题,因为我假设 Java 在调整大小时会重新打印组件。
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.
作为测试,我添加了一个调试按钮,可以在程序启动完成后单击该按钮。我试图强制 JScrollPane 自己“刷新”。
I have tried doing:
我试过这样做:
scrollpane.repaint();
scrollpane.validate();
scrollpane.revalidate();
None of them seems to work. However, if I change the border (or any other layout related to the JScrollPane), it refreshes correctly.
它们似乎都不起作用。但是,如果我更改边框(或与 JScrollPane 相关的任何其他布局),它会正确刷新。
scrollpane.setBorder(new LineBorder(Color.RED));
So I basically have 2 questions.
所以我基本上有2个问题。
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?
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.
强制滚动窗格“刷新”的命令是什么?显然,当我添加边框时,它正在做某种“重绘”。我怎么能只运行它?
有没有办法在添加组件时“暂停”打印,并在添加所有想要的组件后再次恢复打印?就像现在一样,我基本上“看到”了在屏幕上添加的组件(即使它真的很快)。如果我可以添加我想要的所有组件,然后告诉程序将它打印到屏幕/JFrame 上,那就更好了。
采纳答案by camickr
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.
添加组件没有任何作用,因为组件的大小仍然为零,因此没有任何可绘制的内容。当您调用 revalidate() 方法时,布局管理器将被调用,因此组件现在将具有位置/大小。然后 repaint() 将绘制组件。revalidate() 还会在需要时显示滚动条。这当然假设您正在使用布局管理器。
The components are added to the panel so you invoke the methods on the panel, not the scrollpane.
这些组件被添加到面板中,因此您可以调用面板上的方法,而不是滚动窗格。
回答by p6majo
In my case only
仅就我而言
frame.pack();
helped to get the scrollbars on the JScrollPane, when the enclosed JPanel was resized dynamically.
当封闭的 JPanel 动态调整大小时,有助于在 JScrollPane 上获取滚动条。