0

I have a Box (verticalBox) in a panel, with checkboxes inside, and i would to do it scrollable.

I tried with

Box box = Box.createVerticalBox();
JScrollPane scrollArea = new JScrollPane(box,  
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

panel = new JPanel(); 
panel.add(scrollArea); 
add(aggiuntaPanel, BorderLayout.CENTER);

I can see the Box in the frame, but i cannot scroll it. How can i do?

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
Andrean
  • 313
  • 1
  • 4
  • 13

1 Answers1

2

You need to do it the other way round. To make a component scrollable, you have to enclose it inside a JScrollPane.

panel.add(scrollArea);

should be:

scrollPanel.add(panel);

Additionally (as mentioned in the comments by @Gorbels) you will need to set dimensions on the JScrollPane before adding it to the containing component:

scrollPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));

using preferredSize delegates layouting to the layout manager, you may just as well use setSize() though there is some differences as outlined in this SO Q&A

Community
  • 1
  • 1
Vogel612
  • 5,620
  • 5
  • 48
  • 73
  • Additionally, it defaults to zero on the dimensions, so if no dimensions are established it won't show the scrollbar even with the policy set to `VERTICAL_SCROLLBAR_ALWAYS`. You need to set preferred dimensions on at least the `JScrollPane` (if not the `JPanel`). – Gorbles Nov 30 '15 at 14:53
  • @Gorbles thanks for picking that up, I default to setting sizes on anything I create so I overlooked that :) – Vogel612 Nov 30 '15 at 14:59
  • Thank you guys! I did like Vogel said, but now I can't see the panels, I only see the vertical scrollbar – Andrean Nov 30 '15 at 15:03
  • 2
    Well the problem in your code that you didn't post. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Nov 30 '15 at 16:08