0

I'm trying to build an GUI application with Java Swing. There is a button which adds a new JLabel on click. That's working well, but when the number of labels increases more than the panel size, I can't scroll down.

When I tried to add a JScrollPane it also doesn't scroll.

How can I create scroll pane which scrolls down when a new component is added?

Code to add a new label

JLabel label = new JLabel("Hello "+i);
label.setBounds(50, y, 100, 100);
pnl.add(label);
pnl.repaint();
y+=10;
i++;

The design

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    Use a JList inside a JScrollPane instead of trying to add multiple JLabels. This situation is exactly what this component was built for. – Hovercraft Full Of Eels Aug 22 '21 at 13:57
  • 3
    Don't use a null layout. Don't use setBounds(). A scroll pane will not work with a null layout. Your panel needs to use a layout manager. – camickr Aug 22 '21 at 14:03

0 Answers0