[Newbie to Swing]: Alignment question

J

John Curley

Hi, All:

I am writing a Swing app but am new to a lot of the nuances to Swing.

If I have a panel that is using a GridBagLayout, how do I align components
so that they start at the vertical top of the page?

I tried using setAlignmentY(0.0) but that didn't work.

I have that question but I also have a more global question where can I find
resources that help me in component alignment?

Sometimes I need to right align, left align, center, etc.

I have been looking through the Sun site, Sun Swing tutorial and the
internet but not with much success.

Thanks for your help.

Sincerely,
John Curley
 
K

kjc

I would gravitate toward using a nested layout using a combination of
Box ,Border as your top level layouts. In my opinion, GridBagLayout is
VERY difficult to use.
 
A

Andrew Thompson

I am writing a Swing ..

GUI?
..app but am new to a lot of the nuances to Swing.

If I have a panel that is using a GridBagLayout..

GridBagLayout is equally useable (or
unuseable, depending on your views)
in AWT as Swing.
I have been looking through the Sun site,
Sun Swing ..

Wrong tutorial, in this case, try LayoutManagers..
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>

[..and, ironically it has 'uiswing' in
the URL, ..go figure. ]

HTH
 
L

Larry Barowski

John Curley said:
Hi, All:

I am writing a Swing app but am new to a lot of the nuances to Swing.

If I have a panel that is using a GridBagLayout, how do I align components
so that they start at the vertical top of the page?

For a single row that does not fill the horizontal area,
set "anchor" of the GridBagConstraints to
"GridBagConstraints.NORTH" (or NORTHWEST, or
NORTHEAST).

If you have multiple rows and there is no component
in your panel that will fill the horizontal space, the
simplest method is to add an empty panel to the
bottom and give it a high weighty (all other weighty
should be zero, or something like1000 times lower).

You could also give the bottom components a high
weighty and anchor them NORTH, but that may
take more code and require more code modifications
when you change the layout.
 
S

Silvio Bierman

As a Swing newby you should not use GridBagLayout. You could either start
with a much simpler layout manager or start even simpler using
setLayout(null) and using pixel coordinates. Once you get used to other
aspects of Swing like listeners and the models used in trees, listboxes etc.
you can always start looking into layout managers. The SpringLayout would be
a good one to consider for more advanced layouts.

Silvio Bierman
 
A

Andrew Thompson

As a Swing newby you should not use GridBagLayout. You could either start
with a much simpler layout manager or start even simpler using
setLayout(null) and using pixel coordinates.

Wow! I recommand against GBL myself, but feel
*any* layout manager is better than a null layout..

( You are a brave, or foolish, man Silvio! )
 
C

Chris Smith

Silvio said:
As a Swing newby you should not use GridBagLayout. You could either start
with a much simpler layout manager or start even simpler using
setLayout(null) and using pixel coordinates.

Unfortunately, setLayout(null) is far from simple. Writing the code to
lay out components properly with raw pixel locations when you don't know
their proper sizes until runtime is a really difficult thing to do.
Adding the listeners to readjust the layout when the size of the window
changes is also a major pain. A beginner trying to deal with these
issues on their own rather than using a layout manager -- while also
trying to write working software -- is just plain sadistic.

GridBagLayout has a complex interface, but it's far simpler than the
code to come up with a working layout without a layout manager at all.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Liz

Chris Smith said:
Unfortunately, setLayout(null) is far from simple. Writing the code to
lay out components properly with raw pixel locations when you don't know
their proper sizes until runtime is a really difficult thing to do.

I use null, but I know all the sizes in advance. Still it is a lot of work
and if you want to make changes it is almost as bad as starting over.
 
A

Andrew Thompson

I use null, but I know all the sizes in advance.

On all the combinations of default text size/font
etc. that you are likely to encounter using an
X-plat language??

Are you psychic, Liz? Quick ..what's the lotto? ;-)
 
C

Cid

On all the combinations of default text size/font
etc. that you are likely to encounter using an
X-plat language??

Are you psychic, Liz? Quick ..what's the lotto? ;-)

Of course, in fairness, Swing doesn't layout properly on all screens
without serious hackage. Only those where the native dpi is close to
the precious golden 72.

Still I agree it's better than the apps Java and non-Java that assume
the whole world uses the developer's monitor.
 
L

Larry Barowski

Of course, in fairness, Swing doesn't layout properly on all screens
without serious hackage. Only those where the native dpi is close to
the precious golden 72.

But that is due to bugs in the more complex components
(JEditorPane, JFileChooser), not the layout managers.
Most of the dpi-related bugs are fixed in 1.5 Beta 2.

I run at 144 dpi and haven't noticed any problems under
1.5 Beta 2 except that the buttons on the left side of the
JFileChooser with Windows L&Fs get chopped off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top