↓ Skip to main content

JScrollPane does not show scrollbars if child components are resized

·68 words·1 min

Rather annoying:


    icon = new ImageIcon();     
    JLabel label = new JLabel(icon);
    scrollPane = new JScrollPane(label, VERTICAL_SCROLLBAR_AS_NEEDED,
                HORIZONTAL_SCROLLBAR_AS_NEEDED);

    add(scrollPane, CENTER);

the scrollpane has a zero size component. Then if you later set an image to the ImageIcon, the scrollbars do not appear.

To overcome this - call scrollPane.revalidate() after updating the pane’s contents in order to get up-to-date scrollbar sizes.


First published on the old anydoby.com blog on 2007-05-16.