List: fixed endless repainting if table is not focused

This commit is contained in:
Karl Tauber
2019-09-14 22:19:42 +02:00
parent 76f80b6bdf
commit f7c8028243

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.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
@@ -36,6 +37,8 @@ import javax.swing.plaf.basic.BasicListUI;
public class FlatListUI public class FlatListUI
extends BasicListUI extends BasicListUI
{ {
protected Color selectionBackground;
protected Color selectionForeground;
protected Color selectionInactiveBackground; protected Color selectionInactiveBackground;
protected Color selectionInactiveForeground; protected Color selectionInactiveForeground;
@@ -47,32 +50,61 @@ public class FlatListUI
protected void installDefaults() { protected void installDefaults() {
super.installDefaults(); super.installDefaults();
selectionBackground = UIManager.getColor( "List.selectionBackground" );
selectionForeground = UIManager.getColor( "List.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" ); selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" ); selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" );
toggleSelectionColors( list.hasFocus() );
} }
@Override @Override
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( !list.hasFocus() ) { return new BasicListUI.FocusHandler() {
// apply inactive selection background/foreground if list is not focused @Override
Color oldSelectionBackground = list.getSelectionBackground(); public void focusGained( FocusEvent e ) {
Color oldSelectionForeground = list.getSelectionForeground(); super.focusGained( e );
list.setSelectionBackground( selectionInactiveBackground ); toggleSelectionColors( true );
list.setSelectionForeground( selectionInactiveForeground ); }
super.paint( g, c ); @Override
public void focusLost( FocusEvent e ) {
super.focusLost( e );
toggleSelectionColors( false );
}
};
}
list.setSelectionBackground( oldSelectionBackground ); /**
list.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( list.getSelectionBackground() == selectionInactiveBackground )
list.setSelectionBackground( selectionBackground );
if( list.getSelectionForeground() == selectionInactiveForeground )
list.setSelectionForeground( selectionForeground );
} else {
if( list.getSelectionBackground() == selectionBackground )
list.setSelectionBackground( selectionInactiveBackground );
if( list.getSelectionForeground() == selectionForeground )
list.setSelectionForeground( selectionInactiveForeground );
}
} }
} }