Is it possible to tell JPanel to set its size to fit all components that it contains? Something like pack() for JFrame.
edit: The trick with preferredSize didn’t help. I’ve got JSplitPane, where in one part there is GridBagLayout with many labels (see screenshot) and labels overlap each other.
After looking at the source code for
pack(), I came up with:This forces the panel to recalculate its preferred size based on the preferred sizes of its subcomponenents.
You may or may not have to call
validate()afterward; in my tiny example, it seemed to make no difference, but the Javadoc says:So I guess it depends on why you’re having to repack your
JPanel.