• Post Reply Bookmark Topic Watch Topic
  • New Topic

Have a limit of items per row for a JScrollPane?  RSS feed

 
Dan Kao
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I add to a JScrollPane (I'm adding to a JPanel then putting that in the scroll pane) it displays as many items in one row as possible. How can I make it so that it only displays one per row?
Thanks
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are these "items" which you are adding to the JScrollPane? (Or the JPanel, whichever it is.) Did you consider using a JList instead?
 
Dan Kao
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm adding JLabels. Wouldn't the JList only be able to contain items of one type, because I may need to add items of other types later (or would you make its type JComponent instead or something?)

How do you use a JList for this purpose though?
 
Dan Kao
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out I could "cheat" this by centering the JLabel text and setting the label's width to the scrollpane's width.

Thanks for your help though
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default layout for a JPanel is a FlowLayout. It sounds like you are just adding the label to the panel and adding the panel to the scrollpane.

Just change the layout manager of the panel to use a GridLayout with only 1 column. Then each label will bed displayed on its own row.

Playing with sizes of the label is NOT the way to solve the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!