ComboBox: minimum width is now 72 pixels (was ~50 for non-editable and ~130 for editable comboboxes)

This commit is contained in:
Karl Tauber
2020-05-23 22:25:18 +02:00
parent 4af8d2f1c5
commit 0abfb5922a
6 changed files with 38 additions and 1 deletions

View File

@@ -5,6 +5,8 @@ FlatLaf Change Log
- ScrollBar: Made styling more flexible by supporting insets and arc for track - ScrollBar: Made styling more flexible by supporting insets and arc for track
and thumb. (issue #103) and thumb. (issue #103)
- ComboBox: Minimum width is now 72 pixels (was ~50 for non-editable and ~130
for editable comboboxes).
- ComboBox: Support custom borders in combobox editors. (issue #102) - ComboBox: Support custom borders in combobox editors. (issue #102)
- Ubuntu Linux: Fixed poorly rendered font. (issue #105) - Ubuntu Linux: Fixed poorly rendered font. (issue #105)

View File

@@ -99,7 +99,8 @@ public interface FlatClientProperties
/** /**
* Specifies minimum width of a component. * Specifies minimum width of a component.
* <p> * <p>
* <strong>Component</strong> {@link javax.swing.JButton}, {@link javax.swing.JToggleButton} and {@link javax.swing.text.JTextComponent}<br> * <strong>Component</strong> {@link javax.swing.JButton}, {@link javax.swing.JToggleButton},
* {@link javax.swing.JComboBox}, {@link javax.swing.JSpinner} and {@link javax.swing.text.JTextComponent}<br>
* <strong>Value type</strong> {@link java.lang.Integer}<br> * <strong>Value type</strong> {@link java.lang.Integer}<br>
*/ */
String MINIMUM_WIDTH = "JComponent.minimumWidth"; String MINIMUM_WIDTH = "JComponent.minimumWidth";

View File

@@ -78,6 +78,8 @@ import com.formdev.flatlaf.util.UIScale;
* *
* <!-- FlatComboBoxUI --> * <!-- FlatComboBoxUI -->
* *
* @uiDefault ComboBox.minimumWidth int
* @uiDefault ComboBox.editorColumns int
* @uiDefault Component.arrowType String triangle (default) or chevron * @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault Component.isIntelliJTheme boolean * @uiDefault Component.isIntelliJTheme boolean
* @uiDefault Component.borderColor Color * @uiDefault Component.borderColor Color
@@ -96,6 +98,8 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatComboBoxUI public class FlatComboBoxUI
extends BasicComboBoxUI extends BasicComboBoxUI
{ {
protected int minimumWidth;
protected int editorColumns;
protected String arrowType; protected String arrowType;
protected boolean isIntelliJTheme; protected boolean isIntelliJTheme;
protected Color borderColor; protected Color borderColor;
@@ -148,6 +152,8 @@ public class FlatComboBoxUI
LookAndFeel.installProperty( comboBox, "opaque", false ); LookAndFeel.installProperty( comboBox, "opaque", false );
minimumWidth = UIManager.getInt( "ComboBox.minimumWidth" );
editorColumns = UIManager.getInt( "ComboBox.editorColumns" );
arrowType = UIManager.getString( "Component.arrowType" ); arrowType = UIManager.getString( "Component.arrowType" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
borderColor = UIManager.getColor( "Component.borderColor" ); borderColor = UIManager.getColor( "Component.borderColor" );
@@ -247,6 +253,8 @@ public class FlatComboBoxUI
editor.repaint(); editor.repaint();
else if( FlatClientProperties.COMPONENT_ROUND_RECT.equals( propertyName ) ) else if( FlatClientProperties.COMPONENT_ROUND_RECT.equals( propertyName ) )
comboBox.repaint(); comboBox.repaint();
else if( FlatClientProperties.MINIMUM_WIDTH.equals( propertyName ) )
comboBox.revalidate();
} }
}; };
} }
@@ -263,6 +271,7 @@ public class FlatComboBoxUI
Component editor = comboBoxEditor.getEditorComponent(); Component editor = comboBoxEditor.getEditorComponent();
if( editor instanceof JTextField ) { if( editor instanceof JTextField ) {
JTextField textField = (JTextField) editor; JTextField textField = (JTextField) editor;
textField.setColumns( editorColumns );
// assign a non-null and non-javax.swing.plaf.UIResource border to the text field, // assign a non-null and non-javax.swing.plaf.UIResource border to the text field,
// otherwise it is replaced with default text field border when switching LaF // otherwise it is replaced with default text field border when switching LaF
@@ -411,6 +420,13 @@ public class FlatComboBoxUI
return isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground; return isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground;
} }
@Override
public Dimension getMinimumSize( JComponent c ) {
Dimension minimumSize = super.getMinimumSize( c );
minimumSize.width = Math.max( minimumSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) ) );
return minimumSize;
}
@Override @Override
protected Dimension getDefaultSize() { protected Dimension getDefaultSize() {
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
@@ -431,6 +447,18 @@ public class FlatComboBoxUI
Dimension displaySize = super.getDisplaySize(); Dimension displaySize = super.getDisplaySize();
// recalculate width without hardcoded 100 under special conditions
if( displaySize.width == 100 + padding.left + padding.right &&
comboBox.isEditable() &&
comboBox.getItemCount() == 0 &&
comboBox.getPrototypeDisplayValue() == null )
{
int width = getDefaultSize().width;
width = Math.max( width, editor.getPreferredSize().width );
width += padding.left + padding.right;
displaySize = new Dimension( width, displaySize.height );
}
uninstallCellPaddingBorder( renderer ); uninstallCellPaddingBorder( renderer );
return displaySize; return displaySize;
} }

View File

@@ -178,6 +178,8 @@ ColorChooser.swatchesDefaultRecentColor=$control
ComboBox.border=com.formdev.flatlaf.ui.FlatRoundBorder ComboBox.border=com.formdev.flatlaf.ui.FlatRoundBorder
ComboBox.padding=2,6,2,6 ComboBox.padding=2,6,2,6
ComboBox.minimumWidth=72
ComboBox.editorColumns=0
[mac]ComboBox.showPopupOnNavigation=true [mac]ComboBox.showPopupOnNavigation=true

View File

@@ -185,9 +185,11 @@ ComboBox.buttonHoverArrowColor #bbbbbb javax.swing.plaf.ColorUIResource [UI]
ComboBox.buttonShadow #646464 javax.swing.plaf.ColorUIResource [UI] ComboBox.buttonShadow #646464 javax.swing.plaf.ColorUIResource [UI]
ComboBox.disabledBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] ComboBox.disabledBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI]
ComboBox.disabledForeground #777777 javax.swing.plaf.ColorUIResource [UI] ComboBox.disabledForeground #777777 javax.swing.plaf.ColorUIResource [UI]
ComboBox.editorColumns 0
ComboBox.font [active] $defaultFont [UI] ComboBox.font [active] $defaultFont [UI]
ComboBox.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI] ComboBox.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
ComboBox.isEnterSelectablePopup false ComboBox.isEnterSelectablePopup false
ComboBox.minimumWidth 72
ComboBox.noActionOnKeyNavigation false ComboBox.noActionOnKeyNavigation false
ComboBox.padding 2,6,2,6 javax.swing.plaf.InsetsUIResource [UI] ComboBox.padding 2,6,2,6 javax.swing.plaf.InsetsUIResource [UI]
ComboBox.selectionBackground #4b6eaf javax.swing.plaf.ColorUIResource [UI] ComboBox.selectionBackground #4b6eaf javax.swing.plaf.ColorUIResource [UI]

View File

@@ -186,9 +186,11 @@ ComboBox.buttonHoverArrowColor #999999 javax.swing.plaf.ColorUIResource [UI]
ComboBox.buttonShadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] ComboBox.buttonShadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
ComboBox.disabledBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ComboBox.disabledBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI]
ComboBox.disabledForeground #8c8c8c javax.swing.plaf.ColorUIResource [UI] ComboBox.disabledForeground #8c8c8c javax.swing.plaf.ColorUIResource [UI]
ComboBox.editorColumns 0
ComboBox.font [active] $defaultFont [UI] ComboBox.font [active] $defaultFont [UI]
ComboBox.foreground #000000 javax.swing.plaf.ColorUIResource [UI] ComboBox.foreground #000000 javax.swing.plaf.ColorUIResource [UI]
ComboBox.isEnterSelectablePopup false ComboBox.isEnterSelectablePopup false
ComboBox.minimumWidth 72
ComboBox.noActionOnKeyNavigation false ComboBox.noActionOnKeyNavigation false
ComboBox.padding 2,6,2,6 javax.swing.plaf.InsetsUIResource [UI] ComboBox.padding 2,6,2,6 javax.swing.plaf.InsetsUIResource [UI]
ComboBox.selectionBackground #2675bf javax.swing.plaf.ColorUIResource [UI] ComboBox.selectionBackground #2675bf javax.swing.plaf.ColorUIResource [UI]