How do I add a JScrollPane to a JPanel?

There are 20 buttons on the panel. How do I add a slider to this panel so that I can select a button?

UPD: added code from the comment

for (int c = 0; c < 20; c++) { 
    listButtons.add(new JButton(Integer.toString(c)));
    listButtons.get(c).setPreferredSize(new Dimension(100, 100));
    panels[0].add(listButtons.get(c)); 
} 
panels[0].setBorder(BorderFactory.createLineBorder(Color.red‌​)); 
scroller = new JScrollPane(panels[0]); 
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERT‌​ICAL_SCROLLBAR_ALWAY‌​S); 
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HO‌​RIZONTAL_SCROLLBAR_N‌​EVER); 
add(BorderLayout.EAST , scroller);
Author: Nikolai, 2017-09-03

1 answers

Example from Java JPanel inside JScrollPane?:

public class Scroller extends JFrame {

    public Scroller() throws HeadlessException {
        final JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setPreferredSize(new Dimension(800, 600));

        final JScrollPane scroll = new JScrollPane(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Scroller().setVisible(true);
            }
        });
    }
}
 0
Author: Nikolai, 2017-09-03 10:11:44