From bc10c4e871b51bee93f31c32777c9bea34fa018f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 25 Oct 2019 10:20:02 +0200 Subject: [PATCH] Made `JComboBox`, `JProgressBar`, `JSpinner` and `JXDatePicker` non-opaque. `JPasswordField`, `JScrollPane` and `JTextField` are non-opaque if they have an outside focus border (e.g. IntelliJ and Darcula themes). (issues #20 and #17) --- CHANGELOG.md | 4 ++ .../formdev/flatlaf/ui/FlatComboBoxUI.java | 67 ++++++++++--------- .../flatlaf/ui/FlatPasswordFieldUI.java | 10 ++- .../formdev/flatlaf/ui/FlatProgressBarUI.java | 8 +++ .../formdev/flatlaf/ui/FlatScrollPaneUI.java | 6 +- .../com/formdev/flatlaf/ui/FlatSpinnerUI.java | 67 ++++++++++--------- .../formdev/flatlaf/ui/FlatTextFieldUI.java | 24 ++++++- .../com/formdev/flatlaf/ui/FlatUIUtils.java | 9 +++ .../flatlaf/swingx/ui/FlatDatePickerUI.java | 64 +++++++++--------- .../flatlaf/testing/FlatInspector.java | 7 +- .../flatlaf/testing/FlatTestFrame.java | 10 ++- .../flatlaf/testing/FlatTestPanel.java | 24 +++++-- 12 files changed, 194 insertions(+), 106 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37331464..54dbb178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ FlatLaf Change Log - CheckBox: Support painting a third state (set client property "JButton.selectedState" to "indeterminate"). - `TriStateCheckBox` component added (see [FlatLaf Extras](flatlaf-extras)). +- Made `JComboBox`, `JProgressBar`, `JSpinner` and `JXDatePicker` non-opaque. + `JPasswordField`, `JScrollPane` and `JTextField` are non-opaque if they have + an outside focus border (e.g. IntelliJ and Darcula themes). (issues #20 and + #17) ## 0.16 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 eb5227fd..c9516785 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 @@ -41,6 +41,7 @@ import javax.swing.JComponent; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; +import javax.swing.LookAndFeel; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.border.AbstractBorder; @@ -123,6 +124,8 @@ public class FlatComboBoxUI protected void installDefaults() { super.installDefaults(); + LookAndFeel.installProperty( comboBox, "opaque", false ); + focusWidth = UIManager.getInt( "Component.focusWidth" ); arc = UIManager.getInt( "Component.arc" ); arrowType = UIManager.getString( "Component.arrowType" ); @@ -271,44 +274,44 @@ public class FlatComboBoxUI @Override public void update( Graphics g, JComponent c ) { - if( c.isOpaque() ) { + // fill background if opaque to avoid garbage if user sets opaque to true + if( c.isOpaque() ) FlatUIUtils.paintParentBackground( g, c ); - Graphics2D g2 = (Graphics2D) g; - FlatUIUtils.setRenderingHints( g2 ); + Graphics2D g2 = (Graphics2D) g; + FlatUIUtils.setRenderingHints( g2 ); - int width = c.getWidth(); - int height = c.getHeight(); - float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; - float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; - int arrowX = arrowButton.getX(); - int arrowWidth = arrowButton.getWidth(); - boolean enabled = comboBox.isEnabled(); - boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight(); + int width = c.getWidth(); + int height = c.getHeight(); + float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; + float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; + int arrowX = arrowButton.getX(); + int arrowWidth = arrowButton.getWidth(); + boolean enabled = comboBox.isEnabled(); + boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight(); - // paint background - g2.setColor( enabled ? c.getBackground() : disabledBackground ); + // paint background + g2.setColor( enabled ? c.getBackground() : disabledBackground ); + FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + + // paint arrow button background + if( enabled ) { + g2.setColor( comboBox.isEditable() ? buttonEditableBackground : buttonBackground ); + Shape oldClip = g2.getClip(); + if( isLeftToRight ) + g2.clipRect( arrowX, 0, width - arrowX, height ); + else + g2.clipRect( 0, 0, arrowX + arrowWidth, height ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + g2.setClip( oldClip ); + } - // paint arrow button background - if( enabled ) { - g2.setColor( comboBox.isEditable() ? buttonEditableBackground : buttonBackground ); - Shape oldClip = g2.getClip(); - if( isLeftToRight ) - g2.clipRect( arrowX, 0, width - arrowX, height ); - else - g2.clipRect( 0, 0, arrowX + arrowWidth, height ); - FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - g2.setClip( oldClip ); - } - - if( comboBox.isEditable() ) { - // paint vertical line between value and arrow button - g2.setColor( enabled ? borderColor : disabledBorderColor ); - float lw = scale( 1f ); - float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; - g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); - } + // paint vertical line between value and arrow button + if( comboBox.isEditable() ) { + g2.setColor( enabled ? borderColor : disabledBorderColor ); + float lw = scale( 1f ); + float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; + g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); } paint( g, c ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java index ce6e2a06..1a8edddc 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java @@ -60,6 +60,8 @@ public class FlatPasswordFieldUI focusWidth = UIManager.getInt( "Component.focusWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); + LookAndFeel.installProperty( getComponent(), "opaque", focusWidth == 0 ); + MigLayoutVisualPadding.install( getComponent(), focusWidth ); } @@ -87,8 +89,14 @@ public class FlatPasswordFieldUI } @Override - protected void paintBackground( Graphics g ) { + protected void paintSafely( Graphics g ) { FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth ); + super.paintSafely( g ); + } + + @Override + protected void paintBackground( Graphics g ) { + // background is painted elsewhere } @Override diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java index 65a69da6..f4136775 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java @@ -23,6 +23,7 @@ import java.awt.Insets; import java.awt.geom.RoundRectangle2D; import javax.swing.JComponent; import javax.swing.JProgressBar; +import javax.swing.LookAndFeel; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicProgressBarUI; import com.formdev.flatlaf.util.UIScale; @@ -39,6 +40,13 @@ public class FlatProgressBarUI return new FlatProgressBarUI(); } + @Override + protected void installDefaults() { + super.installDefaults(); + + LookAndFeel.installProperty( progressBar, "opaque", false ); + } + @Override protected Dimension getPreferredInnerHorizontal() { return UIScale.scale( super.getPreferredInnerHorizontal() ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java index 439a6521..8580efaf 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java @@ -29,6 +29,7 @@ import java.beans.PropertyChangeEvent; import javax.swing.JComponent; import javax.swing.JScrollPane; import javax.swing.JViewport; +import javax.swing.LookAndFeel; import javax.swing.ScrollPaneLayout; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; @@ -56,7 +57,10 @@ public class FlatScrollPaneUI if( scrollpane.getLayout() instanceof UIResource ) scrollpane.setLayout( new FlatScrollPaneLayout() ); - MigLayoutVisualPadding.install( scrollpane, UIManager.getInt( "Component.focusWidth" ) ); + int focusWidth = UIManager.getInt( "Component.focusWidth" ); + LookAndFeel.installProperty( c, "opaque", focusWidth == 0 ); + + MigLayoutVisualPadding.install( scrollpane, focusWidth ); } @Override 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 53710d7c..09799370 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 @@ -35,6 +35,7 @@ import java.beans.PropertyChangeListener; import javax.swing.JComponent; import javax.swing.JSpinner; import javax.swing.JTextField; +import javax.swing.LookAndFeel; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; @@ -88,6 +89,8 @@ public class FlatSpinnerUI protected void installDefaults() { super.installDefaults(); + LookAndFeel.installProperty( spinner, "opaque", false ); + focusWidth = UIManager.getInt( "Component.focusWidth" ); arc = UIManager.getInt( "Component.arc" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); @@ -225,45 +228,45 @@ public class FlatSpinnerUI @Override public void update( Graphics g, JComponent c ) { - if( c.isOpaque() ) { + // fill background if opaque to avoid garbage if user sets opaque to true + if( c.isOpaque() ) FlatUIUtils.paintParentBackground( g, c ); - Graphics2D g2 = (Graphics2D) g; - FlatUIUtils.setRenderingHints( g2 ); + Graphics2D g2 = (Graphics2D) g; + FlatUIUtils.setRenderingHints( g2 ); - int width = c.getWidth(); - int height = c.getHeight(); - float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; - float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; - Component nextButton = getHandler().nextButton; - int arrowX = nextButton.getX(); - int arrowWidth = nextButton.getWidth(); - boolean enabled = spinner.isEnabled(); - boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight(); + int width = c.getWidth(); + int height = c.getHeight(); + float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; + float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; + Component nextButton = getHandler().nextButton; + int arrowX = nextButton.getX(); + int arrowWidth = nextButton.getWidth(); + boolean enabled = spinner.isEnabled(); + boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight(); - // paint background - g2.setColor( enabled ? c.getBackground() : disabledBackground ); + // paint background + g2.setColor( enabled ? c.getBackground() : disabledBackground ); + FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + + // paint arrow buttons background + if( enabled ) { + g2.setColor( buttonBackground ); + Shape oldClip = g2.getClip(); + if( isLeftToRight ) + g2.clipRect( arrowX, 0, width - arrowX, height ); + else + g2.clipRect( 0, 0, arrowX + arrowWidth, height ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - - // paint arrow buttons background - if( enabled ) { - g2.setColor( buttonBackground ); - Shape oldClip = g2.getClip(); - if( isLeftToRight ) - g2.clipRect( arrowX, 0, width - arrowX, height ); - else - g2.clipRect( 0, 0, arrowX + arrowWidth, height ); - FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - g2.setClip( oldClip ); - } - - // paint vertical line between value and arrow buttons - g2.setColor( enabled ? borderColor : disabledBorderColor ); - float lw = scale( 1f ); - float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; - g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); + g2.setClip( oldClip ); } + // paint vertical line between value and arrow buttons + g2.setColor( enabled ? borderColor : disabledBorderColor ); + float lw = scale( 1f ); + float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; + g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); + paint( g, c ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java index cb6ce1a5..3d5b9c53 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java @@ -25,6 +25,7 @@ import java.awt.event.FocusListener; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JSpinner; +import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicTextFieldUI; @@ -59,6 +60,8 @@ public class FlatTextFieldUI focusWidth = UIManager.getInt( "Component.focusWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); + LookAndFeel.installProperty( getComponent(), "opaque", focusWidth == 0 ); + MigLayoutVisualPadding.install( getComponent(), focusWidth ); } @@ -86,13 +89,30 @@ public class FlatTextFieldUI } @Override - protected void paintBackground( Graphics g ) { + protected void paintSafely( Graphics g ) { paintBackground( g, getComponent(), focusWidth ); + super.paintSafely( g ); + } + + @Override + protected void paintBackground( Graphics g ) { + // background is painted elsewhere } static void paintBackground( Graphics g, JTextComponent c, int focusWidth ) { - FlatUIUtils.paintParentBackground( g, c ); + // do not paint background if: + // - not opaque and + // - border is not a flat border and + // - opaque was explicitly set (to false) + // (same behaviour as in AquaTextFieldUI) + if( !c.isOpaque() && !(c.getBorder() instanceof FlatBorder) && FlatUIUtils.hasOpaqueBeenExplicitlySet( c ) ) + return; + // fill background if opaque to avoid garbage if user sets opaque to true + if( c.isOpaque() && focusWidth > 0 ) + FlatUIUtils.paintParentBackground( g, c ); + + // paint background Graphics2D g2 = (Graphics2D) g.create(); try { FlatUIUtils.setRenderingHints( g2 ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index 541f4839..401d9a35 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -35,6 +35,7 @@ import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.util.function.Consumer; import javax.swing.JComponent; +import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.plaf.ColorUIResource; import com.formdev.flatlaf.util.JavaCompatibility; @@ -248,6 +249,14 @@ public class FlatUIUtils JavaCompatibility.drawStringUnderlineCharAt( c, g, text, underlinedIndex, x, y ); } + public static boolean hasOpaqueBeenExplicitlySet( JComponent c ) { + boolean oldOpaque = c.isOpaque(); + LookAndFeel.installProperty( c, "opaque", !oldOpaque ); + boolean explicitlySet = c.isOpaque() == oldOpaque; + LookAndFeel.installProperty( c, "opaque", oldOpaque ); + return explicitlySet; + } + //---- class HoverListener ------------------------------------------------ public static class HoverListener diff --git a/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatDatePickerUI.java b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatDatePickerUI.java index 2b48c48e..a6b29c80 100644 --- a/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatDatePickerUI.java +++ b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatDatePickerUI.java @@ -103,6 +103,8 @@ public class FlatDatePickerUI super.installUI( c ); + LookAndFeel.installProperty( datePicker, "opaque", false ); + // hack JXDatePicker.TodayPanel colors // (there is no need to uninstall these changes because only UIResources are used, // which are automatically replaced when switching LaF) @@ -217,44 +219,44 @@ public class FlatDatePickerUI @Override public void update( Graphics g, JComponent c ) { - if( c.isOpaque() ) { + // fill background if opaque to avoid garbage if user sets opaque to true + if( c.isOpaque() ) FlatUIUtils.paintParentBackground( g, c ); - Graphics2D g2 = (Graphics2D) g; - FlatUIUtils.setRenderingHints( g2 ); + Graphics2D g2 = (Graphics2D) g; + FlatUIUtils.setRenderingHints( g2 ); - int width = c.getWidth(); - int height = c.getHeight(); - float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; - float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; - int arrowX = popupButton.getX(); - int arrowWidth = popupButton.getWidth(); - boolean enabled = c.isEnabled(); - boolean isLeftToRight = c.getComponentOrientation().isLeftToRight(); + int width = c.getWidth(); + int height = c.getHeight(); + float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; + float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0; + int arrowX = popupButton.getX(); + int arrowWidth = popupButton.getWidth(); + boolean enabled = c.isEnabled(); + boolean isLeftToRight = c.getComponentOrientation().isLeftToRight(); - // paint background - g2.setColor( enabled ? c.getBackground() : disabledBackground ); + // paint background + g2.setColor( enabled ? c.getBackground() : disabledBackground ); + FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + + // paint arrow button background + if( enabled ) { + g2.setColor( buttonBackground ); + Shape oldClip = g2.getClip(); + if( isLeftToRight ) + g2.clipRect( arrowX, 0, width - arrowX, height ); + else + g2.clipRect( 0, 0, arrowX + arrowWidth, height ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - - // paint arrow button background - if( enabled ) { - g2.setColor( buttonBackground ); - Shape oldClip = g2.getClip(); - if( isLeftToRight ) - g2.clipRect( arrowX, 0, width - arrowX, height ); - else - g2.clipRect( 0, 0, arrowX + arrowWidth, height ); - FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - g2.setClip( oldClip ); - } - - // paint vertical line between value and arrow button - g2.setColor( enabled ? borderColor : disabledBorderColor ); - float lw = scale( 1f ); - float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; - g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); + g2.setClip( oldClip ); } + // paint vertical line between value and arrow button + g2.setColor( enabled ? borderColor : disabledBorderColor ); + float lw = scale( 1f ); + float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; + g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); + paint( g, c ); } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatInspector.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatInspector.java index 0d6bbc25..7a349a0d 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatInspector.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatInspector.java @@ -58,6 +58,7 @@ public class FlatInspector private Component lastComponent; private int lastX; private int lastY; + private boolean inspectParent; private JComponent highlightFigure; private JToolTip tip; @@ -70,6 +71,7 @@ public class FlatInspector public void mouseMoved( MouseEvent e ) { lastX = e.getX(); lastY = e.getY(); + inspectParent = e.isShiftDown(); inspect( lastX, lastY ); } } ); @@ -105,6 +107,8 @@ public class FlatInspector private void inspect( int x, int y ) { Container contentPane = rootPane.getContentPane(); Component c = SwingUtilities.getDeepestComponentAt( contentPane, x, y ); + if( inspectParent && c != null && c != contentPane ) + c = c.getParent(); if( c == contentPane || (c != null && c.getParent() == contentPane) ) c = null; @@ -237,7 +241,8 @@ public class FlatInspector } text += "Enabled: " + c.isEnabled() + '\n'; - text += "Opaque: " + c.isOpaque() + '\n'; + text += "Opaque: " + c.isOpaque() + (c instanceof JComponent && + FlatUIUtils.hasOpaqueBeenExplicitlySet( (JComponent) c ) ? " EXPLICIT" : "") + '\n'; text += "Focusable: " + c.isFocusable() + '\n'; text += "Left-to-right: " + c.getComponentOrientation().isLeftToRight() + '\n'; text += "Parent: " + c.getParent().getClass().getName(); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java index b9a08d82..17699d7c 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java @@ -307,7 +307,7 @@ public class FlatTestFrame c.setBackground( explicit ? Color.orange : restoreColor ); } else { c.setForeground( explicit ? Color.blue : restoreColor ); - c.setBackground( explicit ? Color.red : restoreColor ); + c.setBackground( explicit ? Color.green : restoreColor ); } } ); @@ -425,6 +425,14 @@ public class FlatTestFrame contentPanel.getContentPane().remove( content ); content = contentFactory.get(); contentPanel.getContentPane().add( content ); + + if( rightToLeftCheckBox.isSelected() ) + rightToLeftChanged(); + if( !enabledCheckBox.isSelected() ) + enabledChanged(); + if( explicitColorsCheckBox.isSelected() ) + explicitColorsChanged(); + contentPanel.revalidate(); contentPanel.repaint(); } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestPanel.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestPanel.java index a74af0e3..f35ff406 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestPanel.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestPanel.java @@ -29,15 +29,29 @@ public class FlatTestPanel { @Override protected void paintComponent( Graphics g ) { - super.paintComponent( g ); + int width = getWidth(); + int height = getHeight(); - FlatTestFrame frame = (FlatTestFrame) SwingUtilities.getAncestorOfClass( FlatTestFrame.class, this ); - if( frame != null && frame.isPaintBackgroundPattern() ) { + g.setColor( super.getBackground() ); + g.fillRect( 0, 0, width, height ); + + if( isPaintBackgroundPattern() ) { g.setColor( Color.magenta ); - int width = getWidth(); - int height = getHeight(); for( int y = 0; y < height; y += 2 ) g.drawLine( 0, y, width - 1, y ); } } + + /** + * Overridden to see which components paint background with color from parent. + */ + @Override + public Color getBackground() { + return isPaintBackgroundPattern() ? Color.red : super.getBackground(); + } + + private boolean isPaintBackgroundPattern() { + FlatTestFrame frame = (FlatTestFrame) SwingUtilities.getAncestorOfClass( FlatTestFrame.class, this ); + return frame != null && frame.isPaintBackgroundPattern(); + } }