ComboBox: update editor colors correctly when switching LaF

This commit is contained in:
Karl Tauber
2019-08-29 18:35:08 +02:00
parent de476b721c
commit 67fdf90543

View File

@@ -37,6 +37,7 @@ import javax.swing.JPanel;
import javax.swing.ListCellRenderer; import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
@@ -189,13 +190,20 @@ public class FlatComboBoxUI
} }
private void updateEditorColors() { 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(); boolean enabled = editor.isEnabled();
editor.setBackground( enabled ? comboBox.getBackground() : disabledBackground ); editor.setBackground( nonUIResource( enabled ? comboBox.getBackground() : disabledBackground ) );
editor.setForeground( (enabled || editor instanceof JTextComponent) editor.setForeground( nonUIResource( (enabled || editor instanceof JTextComponent)
? comboBox.getForeground() ? comboBox.getForeground()
: disabledForeground ); : disabledForeground ) );
if( editor instanceof JTextComponent ) 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 @Override