Skip to content

Commit 6ec0f43

Browse files
committed
Merge PR #1110: TextField: fixed preferred size when leading/trailing component or icon are present
2 parents 83b621c + fe7ba26 commit 6ec0f43

2 files changed

Lines changed: 53 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ FlatLaf Change Log
1212
- Linux with JetBrains Runtime: Fixed mouse "jumping" to other position when
1313
moving window on scaled secondary screen, if using FlatLaf window decorations.
1414
(issue #1103)
15+
- TextComponents: Fixed preferred width when leading/trailing components or
16+
icons are present. (issue #1110)
1517

1618

1719
## 3.7.1

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)