RFR: 4811995: BorderLayout does not respect minimum size of its components

Andrey Turbanov aturbanov at openjdk.org
Tue Apr 22 21:02:46 UTC 2025


On Sun, 20 Apr 2025 08:19:15 GMT, Jeremy Wood <duke at openjdk.org> wrote:

> This PR changes how the BorderLayout positions components when they don't fit inside a container.
> 
> This should have no effect on BorderLayouts that match or exceed their preferred size.
> 
> (The name of this PR/ticket is a little misleading: this in no way relates to `component.getMinimumSize()`.)
> 
> Here is part of a comment in that ticket that sums up what this PR is trying to fix:
>> Instead of the South Component being placed partially offscreen as Windows does, it is often placed on top of the North Component. Indeed, BorderLayout.layoutContainer() always places the South Component flush with the bottom of the Container and does not check if the Container is smaller than the minimum size, or if the North and South Components overlap.
> 
> Previously child components could:
> A. be assigned negative (x,y) coordinates
> B. be assigned negative widths or heights
> C. overlap other child components
> D. be assigned dimensions that don't fit inside the target container
> 
> This PR will instead constrain certain values. Now child components may be given a width/height of zero pixels, but they should never show the 4 behaviors stated above.
> 
> We encountered this basic problem last week at work (we could end up with a component with a negative height). Our work-around was more complex than this PR: we wrote a modified BorderLayout that would switch to using `component.getMinimumSize()` when the preferred size wouldn't fit. IMO that is a better option all-around, but it is dangerously invasive for an OpenJDK proposal. I'm happy to discuss that idea further, though, if anyone here disagrees.

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 84:

> 82: 
> 83:             return true;
> 84:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 120:

> 118: 
> 119:             return true;
> 120:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 155:

> 153: 
> 154:             return true;
> 155:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 181:

> 179: 
> 180:             return true;
> 181:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 206:

> 204: 
> 205:             return true;
> 206:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

test/jdk/java/awt/BorderLayout/ConstrainedBorderLayoutChildrenTest.java line 232:

> 230: 
> 231:             return true;
> 232:         } catch(Throwable t) {

Suggestion:

        } catch (Throwable t) {

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054863768
PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054863922
PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054864116
PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054864351
PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054864463
PR Review Comment: https://git.openjdk.org/jdk/pull/24772#discussion_r2054864618


More information about the client-libs-dev mailing list