Re: Place JComponent in center of JDialog. Solved



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


.