From 64aa1e8fd1f913c88d381223298efc8cf5b28a80 Mon Sep 17 00:00:00 2001 From: Raven Laing Date: Thu, 2 Apr 2026 00:18:35 +0700 Subject: [PATCH] TextField: fixed extra margin when leading/trailing component or icons are present --- .../formdev/flatlaf/ui/FlatTextFieldUI.java | 77 +++++++++++++------ 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java index 382ee93c6..98ec592d9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java @@ -540,28 +540,41 @@ protected void paintIcons( Graphics g, Rectangle r ) { @Override public Dimension getPreferredSize( JComponent c ) { - return applyMinimumWidth( c, applyExtraSize( super.getPreferredSize( c ) ), minimumWidth ); + return applyMinimumWidth( c, applyExtraSize( super.getPreferredSize( c ), true ), minimumWidth ); } @Override public Dimension getMinimumSize( JComponent c ) { - return applyMinimumWidth( c, applyExtraSize( super.getMinimumSize( c ) ), minimumWidth ); + return applyMinimumWidth( c, applyExtraSize( super.getMinimumSize( c ), false ), minimumWidth ); } - private Dimension applyExtraSize( Dimension size ) { + private Dimension applyExtraSize( Dimension size, boolean reduceInset ) { // add width of leading and trailing icons size.width += getLeadingIconWidth() + getTrailingIconWidth(); // add width of leading and trailing components + boolean leftVisible = false; + boolean rightVisible = false; for( JComponent comp : getLeadingComponents() ) { - if( comp != null && comp.isVisible() ) + if( comp != null && comp.isVisible() ) { size.width += comp.getPreferredSize().width; + leftVisible = true; + } } for( JComponent comp : getTrailingComponents() ) { - if( comp != null && comp.isVisible() ) + if( comp != null && comp.isVisible() ) { size.width += comp.getPreferredSize().width; + rightVisible = true; + } + } + if( reduceInset ) { + boolean ltr = isLeftToRight(); + HorizontalInsets diffInsets = getDiffInsets( leftVisible, rightVisible, ltr ); + if( diffInsets.left > 0 ) + size.width -= diffInsets.left; + if( diffInsets.right > 0 ) + size.width -= diffInsets.right; } - return size; } @@ -587,6 +600,23 @@ private Dimension applyMinimumWidth( JComponent c, Dimension size, int minimumWi return size; } + private HorizontalInsets getDiffInsets( boolean leftVisible, boolean rightVisible, boolean ltr ) { + int left = 0; + int right = 0; + Insets insets = getComponent().getInsets(); + if( leftVisible || (ltr ? hasLeadingIcon() : hasTrailingIcon()) ) { + int newLeftInset = Math.min( insets.left, insets.top ); + if( newLeftInset < insets.left ) + left = insets.left - newLeftInset; + } + if( rightVisible || (ltr ? hasTrailingIcon() : hasLeadingIcon()) ) { + int newRightInset = Math.min( insets.right, insets.top ); + if( newRightInset < insets.right ) + right = insets.right - newRightInset; + } + return new HorizontalInsets( left, right ); + } + static boolean hasDefaultMargins( JComponent c, Insets defaultMargin ) { Insets margin = ((JTextComponent)c).getMargin(); return margin instanceof UIResource && Objects.equals( margin, defaultMargin ); @@ -662,23 +692,13 @@ protected Rectangle getIconsRect() { // if a leading/trailing icons (or components) are shown, then the left/right insets are reduced // to the top inset, which places the icon nicely centered on left/right side - if( leftVisible || (ltr ? hasLeadingIcon() : hasTrailingIcon()) ) { - // reduce left inset - Insets insets = getComponent().getInsets(); - int newLeftInset = Math.min( insets.left, insets.top ); - if( newLeftInset < insets.left ) { - int diff = insets.left - newLeftInset; - r.x -= diff; - r.width += diff; - } - } - if( rightVisible || (ltr ? hasTrailingIcon() : hasLeadingIcon()) ) { - // reduce right inset - Insets insets = getComponent().getInsets(); - int newRightInset = Math.min( insets.right, insets.top ); - if( newRightInset < insets.left ) - r.width += insets.right - newRightInset; + HorizontalInsets diffInsets = getDiffInsets( leftVisible, rightVisible, ltr ); + if( diffInsets.left > 0 ) { + r.x -= diffInsets.left; + r.width += diffInsets.left; } + if( diffInsets.right > 0 ) + r.width += diffInsets.right; // make sure that width and height are not negative r.width = Math.max( r.width, 0 ); @@ -984,4 +1004,17 @@ public void changedUpdate( DocumentEvent e ) { documentChanged( e ); } } + + //---- class HorizontalInsets --------------------------------------------- + + private static class HorizontalInsets + { + int left; + int right; + + HorizontalInsets( int left, int right ) { + this.left = left; + this.right = right; + } + } }