mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-08 15:00:54 +03:00
ComboBox: fixed background colors and padding
This commit is contained in:
@@ -64,8 +64,8 @@ public class FlatButtonUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth();
|
||||
float arc = FlatUIUtils.getButtonArc();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float arc = FlatUIUtils.getButtonArc( c );
|
||||
|
||||
g2.setColor( getBackground( c ) );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
|
||||
|
||||
@@ -18,17 +18,22 @@ package com.formdev.flatlaf.ui;
|
||||
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Color;
|
||||
import java.awt.Container;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.LayoutManager;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.Path2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicArrowButton;
|
||||
import javax.swing.plaf.basic.BasicComboBoxUI;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
/**
|
||||
* Provides the Flat LaF UI delegate for {@link javax.swing.JComboBox}.
|
||||
@@ -42,6 +47,62 @@ public class FlatComboBoxUI
|
||||
return new FlatComboBoxUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
|
||||
padding = UIScale.scale( padding );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LayoutManager createLayoutManager() {
|
||||
return new BasicComboBoxUI.ComboBoxLayoutManager() {
|
||||
@Override
|
||||
public void layoutContainer( Container parent ) {
|
||||
super.layoutContainer( parent );
|
||||
|
||||
if ( editor != null && padding != null ) {
|
||||
// fix editor bounds by subtracting padding
|
||||
editor.setBounds( FlatUIUtils.subtract( editor.getBounds(), padding ) );
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PropertyChangeListener createPropertyChangeListener() {
|
||||
return new BasicComboBoxUI.PropertyChangeHandler() {
|
||||
@Override
|
||||
public void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
|
||||
Object source = e.getSource();
|
||||
String propertyName = e.getPropertyName();
|
||||
|
||||
if( editor != null &&
|
||||
((source == comboBox && propertyName == "background") ||
|
||||
(source == editor && propertyName == "enabled")) )
|
||||
{
|
||||
// fix editor component background color
|
||||
updateEditorBackground();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureEditor() {
|
||||
super.configureEditor();
|
||||
|
||||
updateEditorBackground();
|
||||
}
|
||||
|
||||
private void updateEditorBackground() {
|
||||
editor.setBackground( editor.isEnabled()
|
||||
? comboBox.getBackground()
|
||||
: UIManager.getColor( "ComboBox.disabledBackground" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JButton createArrowButton() {
|
||||
return new FlatArrowButton();
|
||||
@@ -52,30 +113,31 @@ public class FlatComboBoxUI
|
||||
if( c.isOpaque() ) {
|
||||
FlatUIUtils.paintParentBackground( g, c );
|
||||
|
||||
if( c.isEnabled() || comboBox.isEditable() ) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
int width = c.getWidth();
|
||||
int height = c.getHeight();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth();
|
||||
float arc = FlatUIUtils.getComponentArc();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float arc = FlatUIUtils.getComponentArc( c );
|
||||
int arrowX = arrowButton.getX();
|
||||
|
||||
if( c.isEnabled() ) {
|
||||
// paint background
|
||||
g2.setColor( c.getBackground() );
|
||||
g2.setColor( comboBox.isEnabled()
|
||||
? c.getBackground()
|
||||
: UIManager.getColor( "ComboBox.disabledBackground" ) );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
|
||||
|
||||
// paint arrow button background
|
||||
g2.setColor( UIManager.getColor( comboBox.isEditable()
|
||||
g2.setColor( UIManager.getColor( comboBox.isEnabled()
|
||||
? (comboBox.isEditable()
|
||||
? "ComboBox.buttonEditableBackground"
|
||||
: "ComboBox.buttonBackground" ) );
|
||||
: "ComboBox.buttonBackground" )
|
||||
: "ComboBox.disabledBackground" ) );
|
||||
Shape oldClip = g2.getClip();
|
||||
g2.clipRect( arrowX, 0, width - arrowX, height );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
|
||||
g2.setClip( oldClip );
|
||||
}
|
||||
|
||||
if( comboBox.isEditable() ) {
|
||||
// paint vertical line between value and arrow button
|
||||
@@ -83,7 +145,6 @@ public class FlatComboBoxUI
|
||||
g2.fill( new Rectangle2D.Float( arrowX, focusWidth, scale( 1f ), height - (focusWidth * 2) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
paint( g, c );
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public class FlatPasswordFieldUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
|
||||
@@ -63,7 +63,7 @@ public class FlatTextFieldUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
|
||||
@@ -21,6 +21,8 @@ import java.awt.Color;
|
||||
import java.awt.Container;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.geom.Path2D;
|
||||
import java.awt.geom.RoundRectangle2D;
|
||||
@@ -37,6 +39,19 @@ public class FlatUIUtils
|
||||
{
|
||||
public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" );
|
||||
|
||||
public static Rectangle subtract( Rectangle r, Insets insets ) {
|
||||
return new Rectangle(
|
||||
r.x + insets.left,
|
||||
r.y + insets.top,
|
||||
r.width - insets.left - insets.right,
|
||||
r.height - insets.top - insets.bottom );
|
||||
}
|
||||
|
||||
public static Color getUIColor( String key, int defaultColorRGB ) {
|
||||
Color color = UIManager.getColor( key );
|
||||
return (color != null) ? color : new Color( defaultColorRGB );
|
||||
}
|
||||
|
||||
public static int getUIInt( String key, int defaultValue ) {
|
||||
Object value = UIManager.get( key );
|
||||
return (value instanceof Integer) ? (Integer) value : defaultValue;
|
||||
@@ -58,6 +73,18 @@ public class FlatUIUtils
|
||||
return scale( (float) getUIInt( "Button.arc", 6 ) );
|
||||
}
|
||||
|
||||
public static float getFocusWidth( JComponent c ) {
|
||||
return (c.getBorder() instanceof FlatBorder) ? getFocusWidth() : 0;
|
||||
}
|
||||
|
||||
public static float getComponentArc( JComponent c ) {
|
||||
return (c.getBorder() instanceof FlatBorder) ? getComponentArc() : 0;
|
||||
}
|
||||
|
||||
public static float getButtonArc( JComponent c ) {
|
||||
return (c.getBorder() instanceof FlatBorder) ? getButtonArc() : 0;
|
||||
}
|
||||
|
||||
public static Color getBorderColor( boolean enabled, boolean focused ) {
|
||||
return UIManager.getColor( enabled
|
||||
? (focused ? "Component.focusedBorderColor" : "Component.borderColor")
|
||||
|
||||
@@ -20,12 +20,14 @@ import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Insets;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.lang.reflect.Method;
|
||||
import javax.swing.LookAndFeel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.DimensionUIResource;
|
||||
import javax.swing.plaf.InsetsUIResource;
|
||||
import javax.swing.plaf.UIResource;
|
||||
|
||||
/**
|
||||
@@ -195,4 +197,12 @@ public class UIScale
|
||||
? new DimensionUIResource( scale( dimension.width ), scale( dimension.height ) )
|
||||
: new Dimension ( scale( dimension.width ), scale( dimension.height ) ));
|
||||
}
|
||||
|
||||
public static Insets scale( Insets insets ) {
|
||||
return (insets == null || scaleFactor == 1f)
|
||||
? insets
|
||||
: (insets instanceof UIResource
|
||||
? new InsetsUIResource( scale( insets.top ), scale( insets.left ), scale( insets.bottom ), scale( insets.right ) )
|
||||
: new Insets ( scale( insets.top ), scale( insets.left ), scale( insets.bottom ), scale( insets.right ) ));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user