I am creating a GUI with a graphics panel, a command panel and a Command List panel. I\'ve got the command panel where I want it at the bottom of the frame using BorderLayout So
Could anyone explain why the TitledBorder panel is so small?
The size of the text in the border is not used to determine the size of the component. So the width is determined by the preferred size of the component you add to the panel.
So you need to override the getPreferredSize()
method of the panel to return the maximum of the default preferred size calculation or the size of the titled border:
JPanel cmdList = new JPanel()
{
@Override
public Dimension getPreferredSize()
{
Dimension preferredSize = super.getPreferredSize();
Border border = getBorder();
int borderWidth = 0;
if (border instanceof TitledBorder)
{
Insets insets = getInsets();
TitledBorder titledBorder = (TitledBorder)border;
borderWidth = titledBorder.getMinimumSize(this).width + insets.left + insets.right;
}
int preferredWidth = Math.max(preferredSize.width, borderWidth);
return new Dimension(preferredWidth, preferredSize.height);
}
};
Notice how there is still a gap to the right of the "Clear Graphics" Button. Any way to get rid of that?
command.add(Box.createRigidArea(new Dimension(120, 0)));
You just added the rigid area to the command panel so you asked to have the extra 120 pixels at the end.