简体   繁体   English

如何强制刷新/重绘JScrollPane?

[英]How to force-refresh/repaint a 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. 基于数据库中的一些东西,我在程序开始时以编程方式将大量组件(JPanels,JLabels等)添加到JScrollPane中。

It seems that this procedure is too fast for the GUI(?), so the JScrollPane does not always update correctly, ie 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个问题。

  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? 我该怎么办?

  2. 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。

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. 当您调用revalidate()方法时,将调用布局管理器,以便组件现在具有位置/大小。 The repaint() will then paint the components. 然后repaint()将绘制组件。 The revalidate() will also cause the scrollbars to show when required. revalidate()还将导致滚动条在需要时显示。 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. 当封闭的JPanel动态调整大小时,它有助于获取JScrollPane上的滚动条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM