File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Simple side-by-side layout manager? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple side-by-side layout manager?" Watch "Simple side-by-side layout manager?" New topic

Simple side-by-side layout manager?

Harry Wood

Joined: Jan 28, 2004
Posts: 16
I'm fairly new to this LayoutManager stuff, and its driving me nuts. This should be easy enough.

I want to arrange rectangular components side-by-side. The width of each component is the same, and remains fixed. The number of rectangular components (and therefore the total width) will vary dynamically.

I want to do this inside a scroll-pane. When there's lots of them, they will go off the edge, resulting in a horizontal scroll-bar (not flowing). When there's only a few, they should be left justified, still butting up against each other, with space on the right.

Simple enough right? But no matter which layout manager I try, it always does some centering, and spaces them out, instead of keeping them up against each other. Very frustrating.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Harry

Have you tried the following approach:

Have a JPanel with a GridLayout(to make width's even). Add your components to this panel.

Then add this panel to a scroll pane.

Which other managers have you tried?

Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Try JGoodies Forms. It should make what you're trying to do pretty easy.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Scott Delap
Ranch Hand

Joined: Apr 05, 2005
Posts: 73
You might also look into ExplicitLayout. It lets you position components relative to their container and themselves.

Scott Delap
Desktop Java Live
ghazanfar khan
Ranch Hand

Joined: Apr 18, 2005
Posts: 81
Hello Steven Bell ,
I have visited, i have downlaoded Freeware like FormDemo but how can we use this, b/c i am new to this and i need your help please. I run java start web but the form asking some size but there is no button like submit ......

thanks looking forward your feed basck
Venkatraman Kandaswamy
Ranch Hand

Joined: Jul 07, 2004
Posts: 120
Try SGLayout - I have been using this for almost 2 years now and for your case it will definitely work. I highly recommend using SGLayout - simple to use and works !!

--Venkatraman<br />SCJP 1.4<br /><a href="" target="_blank" rel="nofollow">blog</a>
Harry Wood

Joined: Jan 28, 2004
Posts: 16
Thanks for your help everyone. I think I've cracked it, in a roundabout sort of way. As always, the solution has raised new problems, which I'm slowly puzzling through.

GridLayout (as Rachel suggested) and also BoxLayout seemed to fit the bill, but the problem I was having with both of these, was that they seemed to be spacing my components out horizontally, but...

...actually I was confusing myself. The components are JPanels, which in turn contain JButton component. I forgot to set the width on the JPanels, and so they were expanding themselves to take up the available space, while the JButtons (visible) were fixed width. Hence the annoying horizontal centered spacing effect, was resolved by setting the widths. Is there a way to tell a JPanel to choose the smallest possible width? (contract around my JButton, rather than expanding to the layout grid cell width). Currently I've told the JPanel's preffered width to equal to the JButton's preferred width, but it seems to want to make a few pixels of padding, which I dont want. Is there a container padding setting?

The other problem was with verticle centering. I fixed this by putting all my components within another JPanel, then telling the outer panel to be FlowLayout(FlowLayout.LEFT), so that the inner panel was placed in the top left (easier/better way of doing that?)
I agree. Here's the link:
subject: Simple side-by-side layout manager?
Similar Threads
how to add a scrollpane...
Resize Problem
What is the unit of parameters????
JScrollPane, spacing and scrolling issues - HELP!
2 panels in south region