Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I don't want a JPanel to be extended horizontally when adding components to it  RSS feed

 
Abdullah Attia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I've a jScrollPane, inside it there is a JPanel and the JPanel Layout is flowLayout
using code i'm adding Jlabels with image as icon to the JPanel
when i keep adding images when the JLabels meet the horizontal end of the JPanel it's still added horizontally and the JPanel is extended and a horizontal scrollbar appears
But what i want is that when it meets the horizontal end it start new line and not extend the JPanel Horizontally

This is the code that adds images



( it adds the same image as i'm now just designing and testing )

Here is what i get


Here is What i want ( Edited with Paint program )



( Sorry i didn't notice to post this in "Swing / AWT / SWT" forum )
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FlowLayout is not the correct layout manager. The BoxLayout is better by specifying the horizontal dimension. If you using scroll pane, the vertical can be omitted I think.

The only problem with this approach is if you want the panel to use up horizontally when you resize the window ... it won't.
 
Abdullah Attia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying
in fact i was trying to change the layout and this things, and what i want happened
mostly what fixed it for me is assigning preferred size and maximum size the same value (for the JScrollPane and the JPanel)
and it worked in the flow layout, also it still works as expected when re sizing the window
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a JList to display your images. It supports horizontal wrapping.

Check out the section from the Swing tutorial on How to Use Lists for more information and examples.

Or you could use the Wrap Layout which is a custom layout that extends FlowLayout to implement horizontal wrapping more effectively.

mostly what fixed it for me is assigning preferred size and maximum size


You generally don't want to hard code sizes since you lose the dynamic behaviour of the scrolling if you ever add/remove components.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!