Extras: TriStateCheckBox: paint magenta rectangle when used in LaFs that do not support third state

This commit is contained in:
Karl Tauber
2020-05-12 23:26:52 +02:00
parent d3a70b8bb2
commit 5bd40baed2

View File

@@ -16,17 +16,23 @@
package com.formdev.flatlaf.extras; package com.formdev.flatlaf.extras;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatLaf;
/** /**
* A tri-state check box. * A tri-state check box.
* * <p>
* To display the third state, this component requires an LaF that supports painting * To display the third state, this component requires an LaF that supports painting
* the indeterminate state if client property {@code "JButton.selectedState"} has the * the indeterminate state if client property {@code "JButton.selectedState"} has the
* value {@code "indeterminate"}. * value {@code "indeterminate"}.
* * <p>
* FlatLaf and Mac Aqua LaF support the third state. * FlatLaf and Mac Aqua LaF support the third state.
* For other LaFs a magenta rectangle is painted around the component.
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -58,7 +64,7 @@ public class TriStateCheckBox
@Override @Override
public void setSelected( boolean b ) { public void setSelected( boolean b ) {
switch( state ) { switch( state ) {
case INDETERMINATE: setState( State.SELECTED ); break; case INDETERMINATE: setState( State.SELECTED ); break;
case SELECTED: setState( State.UNSELECTED ); break; case SELECTED: setState( State.UNSELECTED ); break;
case UNSELECTED: setState( thirdStateEnabled ? State.INDETERMINATE : State.SELECTED ); break; case UNSELECTED: setState( thirdStateEnabled ? State.INDETERMINATE : State.SELECTED ); break;
} }
@@ -104,4 +110,19 @@ public class TriStateCheckBox
public void setSelected( boolean b ) { public void setSelected( boolean b ) {
setState( b ? State.SELECTED : State.UNSELECTED ); setState( b ? State.SELECTED : State.UNSELECTED );
} }
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
if( !isThirdStateSupported() ) {
g.setColor( Color.magenta );
g.drawRect( 0, 0, getWidth() - 1, getHeight() - 1 );
}
}
private boolean isThirdStateSupported() {
LookAndFeel laf = UIManager.getLookAndFeel();
return laf instanceof FlatLaf || laf.getClass().getName().equals( "com.apple.laf.AquaLookAndFeel" );
}
} }