ComboBox: fixed vertical text alignment (with other components) and component height (when scaled)

This commit is contained in:
Karl Tauber
2019-09-25 11:33:20 +02:00
parent d3ada57a50
commit 95d6d68629
4 changed files with 45 additions and 6 deletions

View File

@@ -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

View File

@@ -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 )

View File

@@ -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,

View File

@@ -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 ----