Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

Place JComponent in center of JDialog

Thread view: 
Martijn Mulder - 29 Jun 2005 08:43 GMT
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 - 29 Jun 2005 17:05 GMT
> 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?

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);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Martijn Mulder - 29 Jun 2005 20:12 GMT
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);
}
}
Knute Johnson - 29 Jun 2005 23:41 GMT
> Thank you Knute for this terse example that I had
> little trouble compiling. I straightened it out a little,
[quoted text clipped - 26 lines]
>  }
> }

Martijn:

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

Signature

Knute Johnson
email s/nospam/knute/

Martijn Mulder - 30 Jun 2005 10:49 GMT
"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 - 01 Jul 2005 00:47 GMT
> "Knute Johnson":
>
[quoted text clipped - 6 lines]
> 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.

To be thread safe for Swing.  See this article:

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

Signature

Knute Johnson
email s/nospam/knute/

rhino2005 - 30 Jun 2005 02:15 GMT
You can use BorderLayout.Center
Andrew Thompson - 30 Jun 2005 05:32 GMT
> 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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.