Re: Place JComponent in center of JDialog
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 09:05:19 -0700
Martijn Mulder wrote:
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);
}
}--
Knute Johnson email s/nospam/knute/ .
- Follow-Ups:
- Re: Place JComponent in center of JDialog. Solved
- From: Martijn Mulder
- Re: Place JComponent in center of JDialog. Solved
- References:
- Place JComponent in center of JDialog
- From: Martijn Mulder
- Place JComponent in center of JDialog
- Prev by Date: Re: FileDialog.setFilenameFilter()
- Next by Date: Re: ProgressMonitor takes too long to display in Sun's ProgressMonitorDemo
- Previous by thread: Place JComponent in center of JDialog
- Next by thread: Re: Place JComponent in center of JDialog. Solved
- Index(es):