From 61c3bbad60a202a6d8b45fc75a5f695025777e40 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 25 Oct 2019 10:28:24 +0200 Subject: [PATCH] ComboBox and Spinner: - make child components explicitly non-opaque - paint parent background only if necessary --- .../java/com/formdev/flatlaf/ui/FlatComboBoxUI.java | 9 ++++++--- .../java/com/formdev/flatlaf/ui/FlatSpinnerUI.java | 12 ++++++++---- 2 files changed, 14 insertions(+), 7 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 c9516785..fc74bb95 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 @@ -212,7 +212,7 @@ public class FlatComboBoxUI String propertyName = e.getPropertyName(); if( editor != null && - ((source == comboBox && (propertyName == "background" || propertyName == "foreground")) || + ((source == comboBox && propertyName == "foreground") || (source == editor && propertyName == "enabled")) ) { // fix editor component colors @@ -242,6 +242,10 @@ public class FlatComboBoxUI if( editor instanceof JTextComponent ) ((JTextComponent)editor).setBorder( BorderFactory.createEmptyBorder() ); + // explicitly make non-opaque + if( editor instanceof JComponent ) + ((JComponent)editor).setOpaque( false ); + editor.applyComponentOrientation( comboBox.getComponentOrientation() ); updateEditorColors(); @@ -252,7 +256,6 @@ public class FlatComboBoxUI // is used, then the editor is updated after the combobox and the // colors are again replaced with default colors boolean enabled = editor.isEnabled(); - editor.setBackground( FlatUIUtils.nonUIResource( enabled ? comboBox.getBackground() : disabledBackground ) ); editor.setForeground( FlatUIUtils.nonUIResource( (enabled || editor instanceof JTextComponent) ? comboBox.getForeground() : disabledForeground ) ); @@ -275,7 +278,7 @@ public class FlatComboBoxUI @Override public void update( Graphics g, JComponent c ) { // fill background if opaque to avoid garbage if user sets opaque to true - if( c.isOpaque() ) + if( c.isOpaque() && (focusWidth > 0 || arc != 0) ) FlatUIUtils.paintParentBackground( g, c ); Graphics2D g2 = (Graphics2D) g; 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 09799370..9994b443 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 @@ -155,6 +155,13 @@ public class FlatSpinnerUI @Override protected JComponent createEditor() { JComponent editor = super.createEditor(); + + // explicitly make non-opaque + editor.setOpaque( false ); + JTextField textField = getEditorTextField( editor ); + if( textField != null ) + textField.setOpaque( false ); + updateEditorColors(); return editor; } @@ -186,8 +193,6 @@ public class FlatSpinnerUI // use non-UIResource colors because when SwingUtilities.updateComponentTreeUI() // is used, then the text field is updated after the spinner and the // colors are again replaced with default colors - textField.setBackground( FlatUIUtils.nonUIResource( spinner.isEnabled() - ? spinner.getBackground() : disabledBackground ) ); textField.setForeground( FlatUIUtils.nonUIResource( spinner.getForeground() ) ); textField.setDisabledTextColor( FlatUIUtils.nonUIResource( disabledForeground ) ); } @@ -229,7 +234,7 @@ public class FlatSpinnerUI @Override public void update( Graphics g, JComponent c ) { // fill background if opaque to avoid garbage if user sets opaque to true - if( c.isOpaque() ) + if( c.isOpaque() && (focusWidth > 0 || arc != 0) ) FlatUIUtils.paintParentBackground( g, c ); Graphics2D g2 = (Graphics2D) g; @@ -373,7 +378,6 @@ public class FlatSpinnerUI @Override public void propertyChange( PropertyChangeEvent e ) { switch( e.getPropertyName() ) { - case "background": case "foreground": case "enabled": updateEditorColors();