diff --git a/CHANGELOG.md b/CHANGELOG.md index be6abd7a..ae1b128b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ FlatLaf Change Log #### Fixed bugs - Button: "selected" state was not shown. (issue #161) +- TextArea: Update background color property if enabled or editable state + changes in the same way as Swing does it for all other text components. (issue + #147) #### Other Changes diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java index 5d640192..9254749f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java @@ -60,6 +60,7 @@ public class FlatTextAreaUI { protected int minimumWidth; protected boolean isIntelliJTheme; + protected Color background; protected Color disabledBackground; protected Color inactiveBackground; @@ -67,12 +68,20 @@ public class FlatTextAreaUI return new FlatTextAreaUI(); } + @Override + public void installUI( JComponent c ) { + super.installUI( c ); + + updateBackground(); + } + @Override protected void installDefaults() { super.installDefaults(); minimumWidth = UIManager.getInt( "Component.minimumWidth" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); + background = UIManager.getColor( "TextArea.background" ); disabledBackground = UIManager.getColor( "TextArea.disabledBackground" ); inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" ); } @@ -81,6 +90,7 @@ public class FlatTextAreaUI protected void uninstallDefaults() { super.uninstallDefaults(); + background = null; disabledBackground = null; inactiveBackground = null; } @@ -89,26 +99,36 @@ public class FlatTextAreaUI protected void propertyChange( PropertyChangeEvent e ) { super.propertyChange( e ); FlatEditorPaneUI.propertyChange( getComponent(), e ); + + switch( e.getPropertyName() ) { + case "editable": + case "enabled": + updateBackground(); + break; + } } - @Override - protected void paintSafely( Graphics g ) { - super.paintSafely( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ) ); - } - - @Override - protected void paintBackground( Graphics g ) { + private void updateBackground() { JTextComponent c = getComponent(); Color background = c.getBackground(); - g.setColor( !(background instanceof UIResource) - ? background - : (isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) - ? FlatUIUtils.getParentBackground( c ) - : (!c.isEnabled() - ? disabledBackground - : (!c.isEditable() ? inactiveBackground : background))) ); - g.fillRect( 0, 0, c.getWidth(), c.getHeight() ); + if( !(background instanceof UIResource) ) + return; + + // do not update background if it currently has a unknown color (assigned from outside) + if( background != this.background && + background != disabledBackground && + background != inactiveBackground ) + return; + + Color newBackground = !c.isEnabled() + ? disabledBackground + : (!c.isEditable() + ? inactiveBackground + : this.background); + + if( newBackground != background ) + c.setBackground( newBackground ); } @Override @@ -128,4 +148,22 @@ public class FlatTextAreaUI return FlatEditorPaneUI.applyMinimumWidth( c, size, minimumWidth ); } + + @Override + protected void paintSafely( Graphics g ) { + super.paintSafely( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ) ); + } + + @Override + protected void paintBackground( Graphics g ) { + JTextComponent c = getComponent(); + + // for compatibility with IntelliJ themes + if( isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) && (c.getBackground() instanceof UIResource) ) { + FlatUIUtils.paintParentBackground( g, c ); + return; + } + + super.paintBackground( g ); + } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java index 5dd8aa44..88169dc1 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java @@ -134,6 +134,7 @@ public class FlatComponentsTest JLabel labelLabel = new JLabel(); JLabel label1 = new JLabel(); JLabel label2 = new JLabel(); + FlatComponentsTest.TestMultiLineLabel testMultiLineLabel1 = new FlatComponentsTest.TestMultiLineLabel(); JLabel buttonLabel = new JLabel(); JButton button1 = new JButton(); JButton button17 = new JButton(); @@ -367,6 +368,10 @@ public class FlatComponentsTest label2.setEnabled(false); add(label2, "cell 2 0"); + //---- testMultiLineLabel1 ---- + testMultiLineLabel1.setText("Multi-line label based on JTextArea\n2nd line"); + add(testMultiLineLabel1, "cell 3 0 2 1"); + //---- buttonLabel ---- buttonLabel.setText("JButton:"); add(buttonLabel, "cell 0 1"); @@ -1402,4 +1407,24 @@ public class FlatComponentsTest return true; } } + + //---- class TestMultiLineLabel ------------------------------------------- + + private static class TestMultiLineLabel + extends JTextArea + { + public TestMultiLineLabel() { + setEditable( false ); + setFocusable( false ); + } + + @Override + public void updateUI() { + super.updateUI(); + setBackground( UIManager.getColor( "Label.background" ) ); + setForeground( UIManager.getColor( "Label.foreground" ) ); + setFont( UIManager.getFont( "Label.font" ) ); + setBorder( null ); + } + } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd index 0a662082..ca3cf281 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd @@ -33,6 +33,12 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 0" } ) + add( new FormComponent( "com.formdev.flatlaf.testing.FlatComponentsTest$TestMultiLineLabel" ) { + name: "testMultiLineLabel1" + "text": "Multi-line label based on JTextArea\n2nd line" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 0 2 1" + } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "buttonLabel" "text": "JButton:"