I tried setting the position of child-panel at the center of parent-panel by using
parent_panel.setLayout(new BorderLayout());
parent_panel.add(child_panel, BorderLayout.CENTER);
But it's getting added at the middle of horizontal screen but vertically at top.
What do I need to do to add it at center of screen vertically and horizontally?
If I understand correctly, you want an interface something like this:
...and you have no other components being added to the parent panel.
If this is the case, you have two choices that I know of (based on this question, which I apparently answered):
Use a
GridBagLayout
with an emptyGridBagConstraints
object, like this:Use a
BoxLayout
, like this: