How to force-refresh/repaint a JScrollPane?

后端 未结 2 1154
夕颜
夕颜 2021-01-12 14:01

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.

It seems th

相关标签:
2条回答
  • 2021-01-12 14:22

    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.

    0 讨论(0)
  • 2021-01-12 14:28

    In my case only

    frame.pack();
    

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

    0 讨论(0)
提交回复
热议问题