@@ -540,41 +540,44 @@ protected void paintIcons( Graphics g, Rectangle r ) {
540540
541541 @ Override
542542 public Dimension getPreferredSize ( JComponent c ) {
543- return applyMinimumWidth ( c , applyExtraSize ( super .getPreferredSize ( c ), true ), minimumWidth );
543+ return applyMinimumWidth ( c , applyExtraSize ( super .getPreferredSize ( c ) ), minimumWidth );
544544 }
545545
546546 @ Override
547547 public Dimension getMinimumSize ( JComponent c ) {
548- return applyMinimumWidth ( c , applyExtraSize ( super .getMinimumSize ( c ), false ), minimumWidth );
548+ return applyMinimumWidth ( c , applyExtraSize ( super .getMinimumSize ( c ) ), minimumWidth );
549549 }
550550
551- private Dimension applyExtraSize ( Dimension size , boolean reduceInset ) {
551+ private Dimension applyExtraSize ( Dimension size ) {
552552 // add width of leading and trailing icons
553553 size .width += getLeadingIconWidth () + getTrailingIconWidth ();
554554
555555 // add width of leading and trailing components
556- boolean leftVisible = false ;
557- boolean rightVisible = false ;
556+ boolean leftCompVisible = false ;
557+ boolean rightCompVisible = false ;
558558 for ( JComponent comp : getLeadingComponents () ) {
559559 if ( comp != null && comp .isVisible () ) {
560560 size .width += comp .getPreferredSize ().width ;
561- leftVisible = true ;
561+ leftCompVisible = true ;
562562 }
563563 }
564564 for ( JComponent comp : getTrailingComponents () ) {
565565 if ( comp != null && comp .isVisible () ) {
566566 size .width += comp .getPreferredSize ().width ;
567- rightVisible = true ;
567+ rightCompVisible = true ;
568568 }
569569 }
570- if ( reduceInset ) {
571- boolean ltr = isLeftToRight ();
572- HorizontalInsets diffInsets = getDiffInsets ( leftVisible , rightVisible , ltr );
573- if ( diffInsets .left > 0 )
574- size .width -= diffInsets .left ;
575- if ( diffInsets .right > 0 )
576- size .width -= diffInsets .right ;
577- }
570+
571+ // if leading/trailing icons or components are shown,
572+ // then the left/right insets are reduced to the top inset,
573+ // which places the icon nicely centered on left/right side
574+ // --> reduce width if necessary
575+ Insets diffInsets = getDiffInsets ( leftCompVisible , rightCompVisible );
576+ if ( diffInsets .left > 0 )
577+ size .width -= diffInsets .left ;
578+ if ( diffInsets .right > 0 )
579+ size .width -= diffInsets .right ;
580+
578581 return size ;
579582 }
580583
@@ -600,23 +603,6 @@ private Dimension applyMinimumWidth( JComponent c, Dimension size, int minimumWi
600603 return size ;
601604 }
602605
603- private HorizontalInsets getDiffInsets ( boolean leftVisible , boolean rightVisible , boolean ltr ) {
604- int left = 0 ;
605- int right = 0 ;
606- Insets insets = getComponent ().getInsets ();
607- if ( leftVisible || (ltr ? hasLeadingIcon () : hasTrailingIcon ()) ) {
608- int newLeftInset = Math .min ( insets .left , insets .top );
609- if ( newLeftInset < insets .left )
610- left = insets .left - newLeftInset ;
611- }
612- if ( rightVisible || (ltr ? hasTrailingIcon () : hasLeadingIcon ()) ) {
613- int newRightInset = Math .min ( insets .right , insets .top );
614- if ( newRightInset < insets .right )
615- right = insets .right - newRightInset ;
616- }
617- return new HorizontalInsets ( left , right );
618- }
619-
620606 static boolean hasDefaultMargins ( JComponent c , Insets defaultMargin ) {
621607 Insets margin = ((JTextComponent )c ).getMargin ();
622608 return margin instanceof UIResource && Objects .equals ( margin , defaultMargin );
@@ -673,26 +659,27 @@ protected Rectangle getIconsRect() {
673659 // remove width of leading/trailing components
674660 JComponent [] leftComponents = ltr ? getLeadingComponents () : getTrailingComponents ();
675661 JComponent [] rightComponents = ltr ? getTrailingComponents () : getLeadingComponents ();
676- boolean leftVisible = false ;
677- boolean rightVisible = false ;
662+ boolean leftCompVisible = false ;
663+ boolean rightCompVisible = false ;
678664 for ( JComponent leftComponent : leftComponents ) {
679665 if ( leftComponent != null && leftComponent .isVisible () ) {
680666 int w = leftComponent .getPreferredSize ().width ;
681667 r .x += w ;
682668 r .width -= w ;
683- leftVisible = true ;
669+ leftCompVisible = true ;
684670 }
685671 }
686672 for ( JComponent rightComponent : rightComponents ) {
687673 if ( rightComponent != null && rightComponent .isVisible () ) {
688674 r .width -= rightComponent .getPreferredSize ().width ;
689- rightVisible = true ;
675+ rightCompVisible = true ;
690676 }
691677 }
692678
693- // if a leading/trailing icons (or components) are shown, then the left/right insets are reduced
694- // to the top inset, which places the icon nicely centered on left/right side
695- HorizontalInsets diffInsets = getDiffInsets ( leftVisible , rightVisible , ltr );
679+ // if leading/trailing icons or components are shown,
680+ // then the left/right insets are reduced to the top inset,
681+ // which places the icon nicely centered on left/right side
682+ Insets diffInsets = getDiffInsets ( leftCompVisible , rightCompVisible );
696683 if ( diffInsets .left > 0 ) {
697684 r .x -= diffInsets .left ;
698685 r .width += diffInsets .left ;
@@ -707,6 +694,27 @@ protected Rectangle getIconsRect() {
707694 return r ;
708695 }
709696
697+ private Insets getDiffInsets ( boolean leftCompVisible , boolean rightCompVisible ) {
698+ int left = 0 ;
699+ int right = 0 ;
700+ boolean ltr = isLeftToRight ();
701+ if ( leftCompVisible || (ltr ? hasLeadingIcon () : hasTrailingIcon ()) ) {
702+ // reduce left inset
703+ Insets insets = getComponent ().getInsets ();
704+ int newLeftInset = Math .min ( insets .left , insets .top );
705+ if ( newLeftInset < insets .left )
706+ left = insets .left - newLeftInset ;
707+ }
708+ if ( rightCompVisible || (ltr ? hasTrailingIcon () : hasLeadingIcon ()) ) {
709+ // reduce right inset
710+ Insets insets = getComponent ().getInsets ();
711+ int newRightInset = Math .min ( insets .right , insets .top );
712+ if ( newRightInset < insets .right )
713+ right = insets .right - newRightInset ;
714+ }
715+ return new Insets ( 0 , left , 0 , right );
716+ }
717+
710718 /** @since 2 */
711719 protected boolean hasLeadingIcon () {
712720 return leadingIcon != null ;
@@ -1004,17 +1012,4 @@ public void changedUpdate( DocumentEvent e ) {
10041012 documentChanged ( e );
10051013 }
10061014 }
1007-
1008- //---- class HorizontalInsets ---------------------------------------------
1009-
1010- private static class HorizontalInsets
1011- {
1012- int left ;
1013- int right ;
1014-
1015- HorizontalInsets ( int left , int right ) {
1016- this .left = left ;
1017- this .right = right ;
1018- }
1019- }
10201015}
0 commit comments