Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Place JComponent in center of JDialog

12 views
Skip to first unread message

Martijn Mulder

unread,
Jun 29, 2005, 3:47:02 AM6/29/05
to
I have a JComponent that displays an image. I set the getPreferredSize() method
to the dimensions of the image, so when I add it to the content pane of a
JDialog, it fits exactly. When I resize the JDialog, I want the image to be
displayed in the exact center of the content pane. Setting the LayoutManager to
FlowLayout.CENTER doesn't work, the position is not vertically adjusted.
BorderLayout("Center") leaves the image in the top left corner. Howto?


Knute Johnson

unread,
Jun 29, 2005, 12:05:19 PM6/29/05
to

You need a LayoutManager that doesn't adjust the size of your component
up to fit the container. GridBagLayout will do that just fine.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test2 {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400,300));
p.setBackground(Color.BLUE);
f.add(p,c);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/

Knute Johnson

unread,
Jun 29, 2005, 6:41:45 PM6/29/05
to
Martijn Mulder wrote:
> Thank you Knute for this terse example that I had
> little trouble compiling. I straightened it out a little,
> (kicked out the thread) and hope that someone Googling
> on 'component+center+parent' will find it someday
>
>
> //How to place a component in the center of its parent
> //Original code by Knute Johnson
> //Adapted for the millions by Martijn Mulder
> //One class test3 is all it takes
> public class test3
> {
>
> //One method main is all it takes

> public static void main(String[] args)
> {
>
> //create JPanel, paint it blue, set preferred size
> javax.swing.JPanel p=new javax.swing.JPanel();
> p.setBackground(java.awt.Color.blue);
> p.setPreferredSize(new java.awt.Dimension(400,300));
>
> //create JFrame, set Layout manager, add JPanel + GridBagConstraints
> javax.swing.JFrame f=new javax.swing.JFrame();
> f.getContentPane().setLayout(new java.awt.GridBagLayout());
> f.getContentPane().add(p,new java.awt.GridBagConstraints());
> f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
> f.pack();
> f.setVisible(true);
> }
> }
>
>

Martijn:

You are very welcome. If I had realized you were using 1.4 I would have
written it differently.

Martijn Mulder

unread,
Jun 29, 2005, 3:17:55 PM6/29/05
to
Thank you Knute for this terse example that I had
little trouble compiling. I straightened it out a little,
(kicked out the thread) and hope that someone Googling
on 'component+center+parent' will find it someday


//How to place a component in the center of its parent
//Original code by Knute Johnson
//Adapted for the millions by Martijn Mulder
//One class test3 is all it takes
public class test3
{

//One method main is all it takes

public static void main(String[] args)
{

//create JPanel, paint it blue, set preferred size

rhino2005

unread,
Jun 29, 2005, 9:15:12 PM6/29/05
to
You can use BorderLayout.Center

Andrew Thompson

unread,
Jun 30, 2005, 12:32:01 AM6/30/05
to
On 29 Jun 2005 18:15:12 -0700, rhino2005 wrote:

> You can use BorderLayout.Center

Sure, but that does not provide the effect Martijn was after.
A BorderLayout.CENTER will put the component in the center
and *stretch* it to fit the available space, as opposed to
putting it in the center and *padding* the outside..

See Knute's solution for exactly what the OP wanted.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane

Martijn Mulder

unread,
Jun 30, 2005, 5:55:02 AM6/30/05
to
"Knute Johnson":

> You are very welcome. If I had realized you were using 1.4 I would have
> written it differently.


Is running your app in a hand-made thread a recommendation in Java 5?
Funky! I tried 5 but java.exe takes forever to start so I switched back to
1.3.1 for faster development circles. I'm stuck with books that cover
java 1.1 and some less-than-perfect java2 references so I'm not really
up-to-date. But code examples have always been my way of learning.


Knute Johnson

unread,
Jun 30, 2005, 7:47:24 PM6/30/05
to

To be thread safe for Swing. See this article:

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

0 new messages