GridLayout was supposed to fill the whole JPanel and do everything in it evenly
these two are opposite forces, the manager can do both at the same time only if the panel width is a multiple of the column width:
panel.width == column.width * column.count
If this is not possible, it maintains the restriction of equal size by determining the size of the children to the largest width to make them equal, and positions the entire block in the center of the parent, thereby exposing the parent background. Basically, you see the effects of integer math :-)
As for the discussion in another answer ,
First: my setXXSize trick is well known - it is always mistakenly used in application code. I think it was a design crash, it should never have happened in the first because most of the time it introduces more problems than it seems (superficially!) , to solve.
The closest solution seems to override getXXSize: if you do this to return an arbitrary fixed fixed size, it is only slightly better than calling setXXSize (if the worst methods are not propagated :): the return value should be related to the internal state. It should contain an internal calculation (if any), which may be intact and may modify it. Plus, strictly speaking, it must correspond to a supercontract that really indicates (a little hidden in setXXSize) that the size applied through setXXSize takes precedence
Sets the preferred size of this component to a constant value. Subsequent calls to getPreferredSize always return this value.
In code:
// always wrong in application code someComponent.setPreferredSize(labelPrefSize); // suboptimal: arbitrary hard-coded fixed size @Override public Dimension getPreferredSize() { return new Dimension(labelPrefSize); } // good: fixed value based on internal state @Override public Dimension getPreferredSize() { return new Dimension(labelPrefSize); } // the size has some meaning fi when painting @Override protected void paintComponent(Graphics g) { g.drawRect(0, 0, labelPrefSize.width, labelPrefSize.height); } // good: prefsize relative to super calculation @Override public Dimension getPreferredSize() { Dimension labelPrefSize = super.getPreferredSize(); int maxSide = Math.max(labelPrefSize.width, labelPrefSize.height); labelPrefSize.setSize(maxSide, maxSide); return labelPrefSize; } // better: prefsize relative to default calculation // _and_ comply to super contract @Override public Dimension getPreferredSize() { Dimension labelPrefSize = super.getPreferredSize(); if (isPreferredSizeSet()) return labelPrefSize; // any of the good thingies ... }
Ideally, however, you would not touch on the internal calculation of prompts. Instead, use the LayoutManager, which allows you to fine-tune the layout to suit your needs. Remember: the manager takes the hints and has the right to size, but she likes it anyway :-)
Update
edited above, trying to emphasize the “related to the internal state”: size tips should return their isolated, egocentric demand, not caring about the context. Therefore, redefinition for the return of something that is like an external necessity is suboptimal.