Table: fixed endless repainting if table is not focused

This commit is contained in:
Karl Tauber
2019-09-14 22:13:44 +02:00
parent a09592cb0d
commit 76f80b6bdf

View File

@@ -17,7 +17,8 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.LookAndFeel; import javax.swing.LookAndFeel;
import javax.swing.UIManager; import javax.swing.UIManager;
@@ -39,6 +40,8 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatTableUI public class FlatTableUI
extends BasicTableUI extends BasicTableUI
{ {
protected Color selectionBackground;
protected Color selectionForeground;
protected Color selectionInactiveBackground; protected Color selectionInactiveBackground;
protected Color selectionInactiveForeground; protected Color selectionInactiveForeground;
@@ -50,9 +53,13 @@ public class FlatTableUI
protected void installDefaults() { protected void installDefaults() {
super.installDefaults(); super.installDefaults();
selectionBackground = UIManager.getColor( "Table.selectionBackground" );
selectionForeground = UIManager.getColor( "Table.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" ); selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" ); selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" );
toggleSelectionColors( table.hasFocus() );
int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 ); int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 );
if( rowHeight > 0 ) if( rowHeight > 0 )
LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) ); LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) );
@@ -62,24 +69,49 @@ public class FlatTableUI
protected void uninstallDefaults() { protected void uninstallDefaults() {
super.uninstallDefaults(); super.uninstallDefaults();
selectionBackground = null;
selectionForeground = null;
selectionInactiveBackground = null; selectionInactiveBackground = null;
selectionInactiveForeground = null; selectionInactiveForeground = null;
} }
@Override @Override
public void paint( Graphics g, JComponent c ) { protected FocusListener createFocusListener() {
if( !table.hasFocus() ) { return new BasicTableUI.FocusHandler() {
// apply inactive selection background/foreground if table is not focused @Override
Color oldSelectionBackground = table.getSelectionBackground(); public void focusGained( FocusEvent e ) {
Color oldSelectionForeground = table.getSelectionForeground(); super.focusGained( e );
table.setSelectionBackground( selectionInactiveBackground ); toggleSelectionColors( true );
table.setSelectionForeground( selectionInactiveForeground ); }
super.paint( g, c ); @Override
public void focusLost( FocusEvent e ) {
super.focusLost( e );
toggleSelectionColors( false );
}
};
}
table.setSelectionBackground( oldSelectionBackground ); /**
table.setSelectionForeground( oldSelectionForeground ); * Toggle selection colors from focused to inactive and vice versa.
} else *
super.paint( g, c ); * This is not a optimal solution but much easier than rewriting the whole paint methods.
*
* Using a LaF specific renderer was avoided because often a custom renderer is
* already used in applications. Then either the inactive colors are not used,
* or the application has to be changed to extend a FlatLaf renderer.
*/
private void toggleSelectionColors( boolean focused ) {
if( focused ) {
if( table.getSelectionBackground() == selectionInactiveBackground )
table.setSelectionBackground( selectionBackground );
if( table.getSelectionForeground() == selectionInactiveForeground )
table.setSelectionForeground( selectionForeground );
} else {
if( table.getSelectionBackground() == selectionBackground )
table.setSelectionBackground( selectionInactiveBackground );
if( table.getSelectionForeground() == selectionForeground )
table.setSelectionForeground( selectionInactiveForeground );
}
} }
} }