@@ -553,15 +553,31 @@ private Dimension applyExtraSize( Dimension size ) {
553553 size .width += getLeadingIconWidth () + getTrailingIconWidth ();
554554
555555 // add width of leading and trailing components
556+ boolean leftCompVisible = false ;
557+ boolean rightCompVisible = false ;
556558 for ( JComponent comp : getLeadingComponents () ) {
557- if ( comp != null && comp .isVisible () )
559+ if ( comp != null && comp .isVisible () ) {
558560 size .width += comp .getPreferredSize ().width ;
561+ leftCompVisible = true ;
562+ }
559563 }
560564 for ( JComponent comp : getTrailingComponents () ) {
561- if ( comp != null && comp .isVisible () )
565+ if ( comp != null && comp .isVisible () ) {
562566 size .width += comp .getPreferredSize ().width ;
567+ rightCompVisible = true ;
568+ }
563569 }
564570
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+
565581 return size ;
566582 }
567583
@@ -643,48 +659,60 @@ protected Rectangle getIconsRect() {
643659 // remove width of leading/trailing components
644660 JComponent [] leftComponents = ltr ? getLeadingComponents () : getTrailingComponents ();
645661 JComponent [] rightComponents = ltr ? getTrailingComponents () : getLeadingComponents ();
646- boolean leftVisible = false ;
647- boolean rightVisible = false ;
662+ boolean leftCompVisible = false ;
663+ boolean rightCompVisible = false ;
648664 for ( JComponent leftComponent : leftComponents ) {
649665 if ( leftComponent != null && leftComponent .isVisible () ) {
650666 int w = leftComponent .getPreferredSize ().width ;
651667 r .x += w ;
652668 r .width -= w ;
653- leftVisible = true ;
669+ leftCompVisible = true ;
654670 }
655671 }
656672 for ( JComponent rightComponent : rightComponents ) {
657673 if ( rightComponent != null && rightComponent .isVisible () ) {
658674 r .width -= rightComponent .getPreferredSize ().width ;
659- rightVisible = true ;
675+ rightCompVisible = true ;
660676 }
661677 }
662678
663- // if a leading/trailing icons (or components) are shown, then the left/right insets are reduced
664- // to the top inset, which places the icon nicely centered on left/right side
665- if ( leftVisible || (ltr ? hasLeadingIcon () : hasTrailingIcon ()) ) {
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 );
683+ if ( diffInsets .left > 0 ) {
684+ r .x -= diffInsets .left ;
685+ r .width += diffInsets .left ;
686+ }
687+ if ( diffInsets .right > 0 )
688+ r .width += diffInsets .right ;
689+
690+ // make sure that width and height are not negative
691+ r .width = Math .max ( r .width , 0 );
692+ r .height = Math .max ( r .height , 0 );
693+
694+ return r ;
695+ }
696+
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 ()) ) {
666702 // reduce left inset
667703 Insets insets = getComponent ().getInsets ();
668704 int newLeftInset = Math .min ( insets .left , insets .top );
669- if ( newLeftInset < insets .left ) {
670- int diff = insets .left - newLeftInset ;
671- r .x -= diff ;
672- r .width += diff ;
673- }
705+ if ( newLeftInset < insets .left )
706+ left = insets .left - newLeftInset ;
674707 }
675- if ( rightVisible || (ltr ? hasTrailingIcon () : hasLeadingIcon ()) ) {
708+ if ( rightCompVisible || (ltr ? hasTrailingIcon () : hasLeadingIcon ()) ) {
676709 // reduce right inset
677710 Insets insets = getComponent ().getInsets ();
678711 int newRightInset = Math .min ( insets .right , insets .top );
679- if ( newRightInset < insets .left )
680- r . width + = insets .right - newRightInset ;
712+ if ( newRightInset < insets .right )
713+ right = insets .right - newRightInset ;
681714 }
682-
683- // make sure that width and height are not negative
684- r .width = Math .max ( r .width , 0 );
685- r .height = Math .max ( r .height , 0 );
686-
687- return r ;
715+ return new Insets ( 0 , left , 0 , right );
688716 }
689717
690718 /** @since 2 */
0 commit comments