Adicionar barra de rolamento ao painel

Estou tentando adicionar o um painel ao scrollpane e add no frame, porem não consigo rolar.
Até aparece as setas porem não aparece a barra de rolamento.

Segue código:

import javax.swing.;
import java.awt.
;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Frame{

static private JFrame frame = new JFrame();
static private JPanel painel = new JPanel();

static int eixoX = 0,eixoY = 0,lojaAnterior = 0;

public static void AbrirFrame(){

    painel.setLayout(new GridBagLayout());
    frame.setSize(900,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    JScrollPane scroll = new JScrollPane(painel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(scroll,BorderLayout.SOUTH);
    frame.getContentPane().add(painel,BorderLayout.WEST);

}
public static void ADDPainel(String loja, JButton box){
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 10, 5);

    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    c.ipadx = 5;
    c.ipady = 5;

    eixoY++;

    ValidarLoja(Integer.parseInt(loja.substring(5)));

    c.gridy = eixoY;
    c.gridx = eixoX;

    painel.repaint();
    painel.revalidate();
    painel.add(box,c);

}

public static void ValidarLoja(int loja){
    if(loja != lojaAnterior){
        eixoX++;
        eixoY = 0;
        lojaAnterior = loja;
    }
}

}

Você está adicionando os dois ao content pane, tá errado.

Tem que adicionar o painel no scroll e o scroll no content pane.

1 curtida

Deu certo, obrigado!.

import javax.swing.;
import java.awt.
;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Frame{

static private JFrame frame = new JFrame("IPTEST");
static private JPanel painel = new JPanel();

static int eixoX = 0,eixoY = 0,lojaAnterior = 0;

public static void AbrirFrame(){

    painel.setLayout(new GridBagLayout());
    frame.setSize(900,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);


    JScrollPane scrollPane = new JScrollPane(painel);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.getVerticalScrollBar().setUnitIncrement(16);
    painel.setBackground(Color.CYAN);

    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    frame.setVisible(true);

}
public static void ADDPainel(String loja, JButton box){
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 10, 5);

    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    c.ipadx = 5;
    c.ipady = 5;

    eixoY++;

    ValidarLoja(Integer.parseInt(loja.substring(5)));

    c.gridy = eixoY;
    c.gridx = eixoX;

    painel.repaint();
    painel.revalidate();
    painel.add(box,c);

}

public static void ValidarLoja(int loja){
    if(loja != lojaAnterior){
        eixoX++;
        eixoY = 0;
        lojaAnterior = loja;
    }
}

}