user1387440
user1387440

Reputation: 11

JScrollPane not scrolling when a JPanel is added

I was hoping someone would be able to help. This seems like it should be a simple problem but for the life of me I can't work it out.

Problem: I am creating a JPanel that is made up of panels containing 5 labels each with ImageIcons. [sounds confusing]

I am then adding this panel to a JScrollPane. But when it is displayed the images are showing and correctly placed but I am unable to scroll down to see the panels that are off the screen.

here is a screenshot: http://img841.imageshack.us/img841/36/screenshot20120510at160.png

Here is the snippet of code I am using to populate the panels and add the JScrollPane.

private void setSeriesViewContainer(){
    container = new BackgroundPanel(backGround, BackgroundPanel.TILED);
    //container.setPreferredSize(new Dimension(650,500));
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
    FlowLayout flowLayout = new FlowLayout();
    JPanel[] jp = new BackgroundPanel[10];
        for (int i = 0; i < jp.length; i++) {
        jp[i] = new BackgroundPanel(backGround, BackgroundPanel.TILED);
        jp[i].setLayout(flowLayout);
            for (int j = 0; j < 10; j++) {
                jp[i].add(new JLabel(new         ImageIcon(getClass().getResource("/placeHolder.png"))));

            }

    }
        for (int i = 0; i < jp.length; i++) {
        container.add(jp[i]);

    }
public void init(){
seriesViewContainer = new javax.swing.JScrollPane(container);
seriesViewContainer.setBorder(null);
         seriesViewContainer.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    seriesViewContainer.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    seriesViewContainer.setPreferredSize(new java.awt.Dimension(700, 300));}

I have searched around for the solution but have not come up with one as yet.

Upvotes: 1

Views: 4992

Answers (2)

porfiriopartida
porfiriopartida

Reputation: 1556

Have you tried to call revalidate() to the JScrollPane and/or container after each add ?

Upvotes: 0

Itchy Nekotorych
Itchy Nekotorych

Reputation: 992

container.setPreferredSize(new Dimension(x,y)); the dimensions of container should be larger than the dimensions of the scrollpane.

from what I've read setPreferredSize() is not a good thing to use though. The problem is probably the LayoutManager for container or jp.

same problem here: Java Swing: JScrollPane not working

Upvotes: 2

Related Questions