Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force JScrollPane to only scroll vertical?

Guys, I need to put some buttons in a jscrollpanel, but the JScrollPane won't create a scroll vertically. I'm using a JPanel inside the JScrollPane which is using the simple FlowLayout layout. How can I make the JScrollPanel to scroll only in the vertical??

Problem:

enter image description here

Desired Solution: enter image description here

like image 734
Marcos Roriz Junior Avatar asked Jan 24 '11 14:01

Marcos Roriz Junior


People also ask

How do I make an image scroll vertically in HTML?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

What is the difference between a scrollbar and a JScrollPane?

What is the difference between a Scrollbar and a JScrollPane ? A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.


2 Answers

Check out the Wrap Layout

like image 79
camickr Avatar answered Sep 23 '22 18:09

camickr


The fact you use a JScrollPane changes quite a few things concerning the internal FlowLayout. indeed, when the FlowLayout tries to layout contained JButtons, it use for that the available space. In your case, you don't have limits to the space in the "scrollable client" of your JScrollPane. As a consequence, considering your FlowLayout has infinite space, it uses this space to display items according to it.

So the solution would be to change your scrollable client in order to limit its viewable area to the same than your JScrollPane's JViewport.

However, you would not even in this case have your line returns, as FlowLayout don't really well handle this case.

Were I to be you, I would of course choose an other layout. As GridLayout don't really well handles borders, i think the only reasonible standard layout you can use is GridBagLayout, althgough I fear your dynamic content constraints may require you something even more customizable.

like image 41
Riduidel Avatar answered Sep 22 '22 18:09

Riduidel