Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple side-by-side layout manager?

 
Harry Wood
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Cheers,
Rachel
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try JGoodies Forms. It should make what you're trying to do pretty easy.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Scott Delap
author
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also look into ExplicitLayout. It lets you position components relative to their container and themselves.
http://www.zookitec.com/explicitlayout.html

Scott Delap
ClientJava.com
Desktop Java Live
 
ghazanfar khan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steven Bell ,
I have visited jgoodies.com, 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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Harry Wood
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic