From 95d6d68629e10099acf05b9dbab15e6769f51e01 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 25 Sep 2019 11:33:20 +0200 Subject: [PATCH] ComboBox: fixed vertical text alignment (with other components) and component height (when scaled) --- .../formdev/flatlaf/ui/FlatComboBoxUI.java | 35 +++++++++++++++++-- .../com/formdev/flatlaf/ui/FlatSpinnerUI.java | 4 +-- .../com/formdev/flatlaf/ui/FlatUIUtils.java | 10 +++++- .../com/formdev/flatlaf/FlatLaf.properties | 2 +- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index 50e14a72..a97fdcbf 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -20,8 +20,10 @@ import static com.formdev.flatlaf.util.UIScale.scale; import java.awt.Color; import java.awt.Component; import java.awt.Container; +import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Insets; import java.awt.LayoutManager; import java.awt.Rectangle; import java.awt.Shape; @@ -138,7 +140,7 @@ public class FlatComboBoxUI if ( editor != null && padding != null ) { // fix editor bounds by subtracting padding - editor.setBounds( FlatUIUtils.subtract( editor.getBounds(), padding ) ); + editor.setBounds( FlatUIUtils.subtractInsets( editor.getBounds(), padding ) ); } } }; @@ -282,7 +284,13 @@ public class FlatComboBoxUI boolean shouldValidate = (c instanceof JPanel); if( padding != null ) - bounds = FlatUIUtils.subtract( bounds, padding ); + bounds = FlatUIUtils.subtractInsets( bounds, padding ); + + // increase the size of the rendering area to make sure that the text + // is vertically aligned with other component types (e.g. JTextField) + Insets rendererInsets = getRendererComponentInsets( c ); + if( rendererInsets != null ) + bounds = FlatUIUtils.addInsets( bounds, rendererInsets ); currentValuePane.paintComponent( g, c, comboBox, bounds.x, bounds.y, bounds.width, bounds.height, shouldValidate ); } @@ -293,6 +301,29 @@ public class FlatComboBoxUI g.fillRect( bounds.x, bounds.y, bounds.width, bounds.height ); } + @Override + protected Dimension getSizeForComponent( Component comp ) { + Dimension size = super.getSizeForComponent( comp ); + + // remove the renderer border top/bottom insets from the size to make sure that + // the combobox gets the same height as other component types (e.g. JTextField) + Insets rendererInsets = getRendererComponentInsets( comp ); + if( rendererInsets != null ) + size = new Dimension( size.width, size.height - rendererInsets.top - rendererInsets.bottom ); + + return size; + } + + private Insets getRendererComponentInsets( Component rendererComponent ) { + if( rendererComponent instanceof JComponent ) { + Border rendererBorder = ((JComponent)rendererComponent).getBorder(); + if( rendererBorder != null ) + return rendererBorder.getBorderInsets( rendererComponent ); + } + + return null; + } + //---- class FlatComboPopup ----------------------------------------------- private class FlatComboPopup diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java index 597c83b6..2eebca06 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java @@ -315,7 +315,7 @@ public class FlatSpinnerUI public void layoutContainer( Container parent ) { Dimension size = parent.getSize(); Insets insets = parent.getInsets(); - Rectangle r = FlatUIUtils.subtract( new Rectangle( size ), insets ); + Rectangle r = FlatUIUtils.subtractInsets( new Rectangle( size ), insets ); if( nextButton == null && previousButton == null ) { if( editor != null ) @@ -339,7 +339,7 @@ public class FlatSpinnerUI } if( editor != null ) - editor.setBounds( FlatUIUtils.subtract( editorRect, padding ) ); + editor.setBounds( FlatUIUtils.subtractInsets( editorRect, padding ) ); int nextHeight = Math.round( buttonsRect.height / 2f ); if( nextButton != null ) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index 8fd9be27..7edee9ef 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -45,7 +45,15 @@ public class FlatUIUtils { public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" ); - public static Rectangle subtract( Rectangle r, Insets insets ) { + public static Rectangle addInsets( Rectangle r, Insets insets ) { + return new Rectangle( + r.x - insets.left, + r.y - insets.top, + r.width + insets.left + insets.right, + r.height + insets.top + insets.bottom ); + } + + public static Rectangle subtractInsets( Rectangle r, Insets insets ) { return new Rectangle( r.x + insets.left, r.y + insets.top, diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index bdf7653d..bb96e010 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -97,7 +97,7 @@ ColorChooser.swatchesRecentSwatchSize=16,16 #---- ComboBox ---- ComboBox.border=com.formdev.flatlaf.ui.FlatRoundBorder -ComboBox.padding=1,6,1,6 +ComboBox.padding=2,6,2,6 #---- Component ----