Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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 )
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • 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
 
Rancher
Posts: 3191
29
  • 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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic