I have the next code:
public class MyScroll extends JFrame {
public MyScroll() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton b = new JButton("Hello-" + i);
b.setBounds(0, i * 50, 100, 45);
panel.add(b);
b.setLayout(null);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(50, 30, 100, 325);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.setBackground(Color.BLUE);
contentPane.add(scrollPane);
contentPane.setLayout(null);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
And, it is rendering this:
As you can see, the vertical and horizontal scroll aren't working, but both are defined and are displaying inside the JPanel
.
Can someone explain me what am I doing wrong?
This code is based on this one: Scrolling a JPanel But, the is not working at the moment to use the verticall scrolling