简体   繁体   中英

JPane inside JScrolledPane. No vertical scrollbar when needed

ive got a JPane within a JScrolledPane. When i add content to JPane , JScrollPane doesnt show scrollbar. I tried repaint() and revalidate() but it didnt help.

static void ladowaniePaneli()
    {
    int b;
    for(b=0;b<o;b++)
        {
        bgPanel[b] = new JBackgroundPanel();
        nowyPanel[b] = new JPanel();

        ((FlowLayout)bgPanel[b].getLayout()).setVgap(0);
        nowyPanel[b].setPreferredSize(new Dimension(790,518));
        nowyPanel[b].setOpaque(false);

        vertical[b] = new JScrollPane(nowyPanel[b]);
        vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        vertical[b].setPreferredSize(new Dimension(789,517));
        vertical[b].setOpaque(false);
        vertical[b].getViewport().setOpaque(false);
        bgPanel[b].add(vertical[b]);           
        }
    }

It makes sense that scrollbars are never seen since you restrict the size of the contained component so that it's always trivially larger than the scrollopane's viewport:

nowyPanel[b].setPreferredSize(new Dimension(790,518));

Solution: don't do that.

if i dont use setPreferredSize method components wont warp to another line

You can try the Wrap Layout .

pairs should be warped to new line if they exceed JScrollPane width

Components are layed out individually. I you want a group of components to wrap then you would need to add the components to a separate panel first. Then add the panel to the panel using the WrapLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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