You can build you own LayoutManager to center a single component(both axis or just one). Here is the one which does it on both axis, you can easily change it to have vertical or horizontal centering.
The current implementation layouts first visible child, you can change that too...
public class CentreLayout implements LayoutManager, java.io.Serializable {
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container target) {
return target.getPreferredSize();
}
public Dimension minimumLayoutSize(Container target) {
return target.getMinimumSize();
}
public void layoutContainer(Container target) {
synchronized (target.getTreeLock()) {
Insets insets = target.getInsets();
Dimension size = target.getSize();
int w = size.width - (insets.left + insets.right);
int h = size.height - (insets.top + insets.bottom);
int count = target.getComponentCount();
for (int i = 0; i < count; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
m.setBounds((w - d.width) / 2, (h - d.height) / 2, d.width, d.height);
break;
}
}
}
}
}