Hi,
I am using Swing and JDK 1.4.
I would like to have a component (a JTextField, for example) centered
inside a JPanel, keeping its preferred size.
FlowLayout on the panel makes the component to be horizontally centered,
but I also want it to be vertically centered.
BorderLayout makes the component resize.
Any idea?
Graham Perks - 08 Jan 2004 14:09 GMT
using Swing and JDK 1.4.
> I would like to have a component (a JTextField, for example) centered
> inside a JPanel, keeping its preferred size.
That sounds like a job for Foam! That is so simple using Foam. Simply
don't give the component any sizing anchors, and tell Foam to Center
Horizontally and Vertically. That's it. Plus, one line of code will
load up the JPanel - no more messy layout code to maintain!
Check it out at http://www.computersinmotion.com - the Introduction
movie shows OK and Cancel buttons being centered, horizontally only but
vertically is only one more menu away!
--
Cheers,
Graham Perks.
ak - 08 Jan 2004 14:10 GMT
> I would like to have a component (a JTextField, for example) centered
> inside a JPanel, keeping its preferred size.
a) write your own LayoutManager
b) [if you have only one component] forget LayoutManager (set it to null)
and add ComponentListener to your JPanel which should compute and set right
location
____________
http://reader.imagero.com the best java image reader.
Jon A. Cruz - 14 Jan 2004 19:22 GMT
> b) [if you have only one component] forget LayoutManager (set it to null)
> and add ComponentListener to your JPanel which should compute and set right
> location
This should never really be done. Don't set the layout null.
Jim Sculley - 08 Jan 2004 17:15 GMT
> Hi,
>
[quoted text clipped - 7 lines]
>
> BorderLayout makes the component resize.
GridBagLayout will do this. In fact, it is the default behavior when
you have a single item in a container using GBL.
Jim S.

Signature
Remove my extraneous mandibular appendages to reply via e-mail
Jon A. Cruz - 14 Jan 2004 19:45 GMT
> Hi,
>
[quoted text clipped - 9 lines]
>
> Any idea?
Yes. GridBagLayout.
By default, a new GridBagConstraints object is initialized to do exactly
what you want (Anchor CENTER, fill NONE, etc)
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
Here's a simple example:
JFrame frame = new JFrame();
JLabel label = new JLabel( "Foo" );
// These next two are just to make it easier to see what's going on
label.setBackground( Color.green );
label.setOpaque( true );
// Now for the actual work
GridBagConstraints constr = new GridBagConstraints();
frame.getContentPane().setLayout( new GridBagLayout() );
frame.getContentPane().add( label, constr );
// Now to see what happens
frame.pack();
frame.show();
Enne - 03 Feb 2004 00:25 GMT
Suppose that instead of a JLabel you have a JScrollbar inside the JPanel visualizing the same JLabel.( lets call the JLabel "Content" as it could be any JComponent )
If the resulting "Content" of the JScrollbar is smaller than the container allocated space then i got the "Content" nicely visualized in the center of the JPanel..but if it's bigger i got a tiny tiny jscrollbar visualized and centered in the JPanel.
In this case i'd like to have the JScrollbar to be at least as large as the container...can your example modifed to obtain such ( reasonable ) behaviour?
Thanks in advance!
Chris Smith - 03 Feb 2004 02:22 GMT
> Suppose that instead of a JLabel you have a JScrollbar inside the JPanel visualizing the same JLabel.( lets call the JLabel "Content" as it could be any JComponent )
> If the resulting "Content" of the JScrollbar is smaller than the container allocated space then i got the "Content" nicely visualized in the center of the JPanel..but if it's bigger i got a tiny tiny jscrollbar visualized and centered in the JPanel.
> In this case i'd like to have the JScrollbar to be at least as large as the container...can your example modifed to obtain such ( reasonable ) behaviour?
Please set your news readers to a sane setting for line wrap.
In any case, if you set the layout of the JPanel to BorderLayout and add
the JScrollPane as a center component, the JScrollPane will always be at
least the size of the container.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Enne - 03 Feb 2004 21:17 GMT
> In any case, if you set the layout of the JPanel to
> BorderLayout and add the JScrollPane as a center
> component, the JScrollPane will always be at
> least the size of the container.
But then the "Content" visualized by the JScrollPanel
won't be centered in the JPanel if it's smaller than
the space the JPanel will obtain.