Marcos Roriz Junior
Marcos Roriz Junior

Reputation: 4106

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

Upvotes: 14

Views: 15465

Answers (6)

SuperRetro
SuperRetro

Reputation: 91

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.

Upvotes: 0

RainMain
RainMain

Reputation: 49

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.

Upvotes: 4

camickr
camickr

Reputation: 324207

Check out the Wrap Layout

Upvotes: 13

jzd
jzd

Reputation: 23639

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.

Upvotes: 3

Riduidel
Riduidel

Reputation: 22308

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.

Upvotes: 4

emeraldjava
emeraldjava

Reputation: 11212

scrollbar = new Scrollbar(Scrollbar.VERTICAL);

Upvotes: 1

Related Questions