I have a custom GUI compomemt, which is based on Swing's JPanel. This component is placed in a JFrame, that uses BorderLayout. When I resize the frame, this component keeps resizing. How can I avoid this? I would like the component to keep the same size whatever happens. I've tried setSize, setPreferredSize, setMinimumSize with no success.
Thanks in advance!
M
You have a few options:
Nest the component in an inner panel with a
LayoutManager
that does not resize your componentUse a more sophisticated LayoutManager than
BorderLayout
. Seems to me likeGridBagLayout
would suit your needs better here.Example of the first solution:
Result:
Green component placed in
BorderLayout.CENTER
, red component maintains preferred size.if you are using custom layout manager, change your current layout to GridBagLayout and change fill options to NONE, after that change it back to your first layout.