From 67fdf90543af0ce7f9c32a00917dc1b80ae8cc02 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 29 Aug 2019 18:35:08 +0200 Subject: [PATCH] ComboBox: update editor colors correctly when switching LaF --- .../com/formdev/flatlaf/ui/FlatComboBoxUI.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 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 0e356e38..cc60a5e9 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 @@ -37,6 +37,7 @@ import javax.swing.JPanel; import javax.swing.ListCellRenderer; import javax.swing.SwingConstants; import javax.swing.UIManager; +import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.text.JTextComponent; @@ -189,13 +190,20 @@ public class FlatComboBoxUI } private void updateEditorColors() { + // use non-UIResource colors because when SwingUtilities.updateComponentTreeUI() + // 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( enabled ? comboBox.getBackground() : disabledBackground ); - editor.setForeground( (enabled || editor instanceof JTextComponent) + editor.setBackground( nonUIResource( enabled ? comboBox.getBackground() : disabledBackground ) ); + editor.setForeground( nonUIResource( (enabled || editor instanceof JTextComponent) ? comboBox.getForeground() - : disabledForeground ); + : disabledForeground ) ); if( editor instanceof JTextComponent ) - ((JTextComponent)editor).setDisabledTextColor( disabledForeground ); + ((JTextComponent)editor).setDisabledTextColor( nonUIResource( disabledForeground ) ); + } + + private Color nonUIResource( Color c ) { + return (c instanceof ColorUIResource) ? new Color( c.getRGB(), true ) : c; } @Override