From 420818261cb92edf180da8ec64c3baac8873c942 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 26 Aug 2019 20:31:26 +0200 Subject: [PATCH] FlatTestFrame: fixed restore of default colors --- .../com/formdev/flatlaf/FlatTestFrame.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatTestFrame.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatTestFrame.java index 737bce66..a4fb42dc 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatTestFrame.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatTestFrame.java @@ -20,6 +20,7 @@ import java.awt.*; import java.awt.event.KeyEvent; import java.util.prefs.Preferences; import javax.swing.*; +import javax.swing.plaf.ColorUIResource; import net.miginfocom.swing.*; /** @@ -165,30 +166,28 @@ public class FlatTestFrame } private void explicitColorsChanged() { - boolean explicit = explicitColorsCheckBox.isSelected(); + EventQueue.invokeLater( () -> { + boolean explicit = explicitColorsCheckBox.isSelected(); + ColorUIResource restoreColor = new ColorUIResource( Color.white ); - for( Component c : content.getComponents() ) { - c.setForeground( explicitColor( explicit, c, "foreground", Color.blue ) ); - c.setBackground( explicitColor( explicit, c, "background", Color.red ) ); + for( Component c : content.getComponents() ) { + c.setForeground( explicit ? Color.blue : restoreColor ); + c.setBackground( explicit ? Color.red : restoreColor ); - if( c instanceof JScrollPane ) { - Component view = ((JScrollPane)c).getViewport().getView(); - if( view != null ) { - view.setForeground( explicitColor( explicit, view, "foreground", Color.magenta ) ); - view.setBackground( explicitColor( explicit, view, "background", Color.orange ) ); + if( c instanceof JScrollPane ) { + Component view = ((JScrollPane)c).getViewport().getView(); + if( view != null ) { + view.setForeground( explicit ? Color.magenta : restoreColor ); + view.setBackground( explicit ? Color.orange : restoreColor ); + } } } - } - } - private Color explicitColor( boolean explicit, Component c, String propertyName, Color explicitColor ) { - if( explicit ) - return explicitColor; - else { - String uiClassID = ((JComponent)c).getUIClassID(); - String key = uiClassID.substring( 0, uiClassID.length() - 2 ) + "." + propertyName; - return UIManager.getColor( key ); - } + // because colors may depend on state (e.g. disabled JTextField) + // it is best to update all UI delegates to get correct result + if( !explicit ) + SwingUtilities.updateComponentTreeUI( content ); + } ); } private void rightToLeftChanged() {