14

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

jzd
  • 23,473
  • 9
  • 54
  • 76
Marcos Roriz Junior
  • 4,076
  • 11
  • 51
  • 76

6 Answers6

13

Check out the Wrap Layout

camickr
  • 321,443
  • 19
  • 166
  • 288
4

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.

Riduidel
  • 22,052
  • 14
  • 85
  • 185
4
JTextArea c = new JTextArea();
c.setLineWrap(true);
c.setWrapStyleWord(false);

This will wrap anything in a text area to the next line without creating a Horizontal Scroll.

RainMain
  • 49
  • 1
  • 1
3

Use the modified Flow Layout that I posted in this answer: How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?

It will wrap to the next line and your scrollbar should scroll vertically.

Community
  • 1
  • 1
jzd
  • 23,473
  • 9
  • 54
  • 76
1
scrollbar = new Scrollbar(Scrollbar.VERTICAL);
emeraldjava
  • 10,894
  • 26
  • 97
  • 170
  • This does absolutely nothing to solve the problem, which is about layout, not creating a scrollbar. The OP already has a scrollbar, provided by `JScrollPanel`. – Nateowami Aug 24 '15 at 06:58
0

Or you could use a JList.

See this site for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

the example class: ListDialog uses only a vertical scrollbar, when the window is resized or the elements don't fit the view.

SuperRetro
  • 91
  • 6